Big O Notation

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the logic for a pivot helper function when coding quick sort?

- Accept 3 arguments (arrary, start index, end index). - Grab the pivot from the start of the array. (median is optimal) - Store the current pivot index. - Loop through the array from the start to end. - If the pivot is greater than the current element, increment the pivot index variable, then swap the current element with the element at the pivot index. - Swap the starting element (pivot) with the pivot index. - Return pivot index.

What is the logic for sorting in the merge sort algorithm?

- Break up the array into halves until you have arrays that are empty or have one element (recursively); - Once you have smaller sorted arrays, merge those arrays with other sorted arrays until you are back to the full length of the array. - Once array has been merged, return the merged and sorted array.

What is the Big O of Binary heaps?

- Insertion: O(log n) - Deletion: O(log n) - Search: O(N)

How does the JS sort method work?

- It accepts an optional comparator function. - You can use this comparator function to tell JS how you want it to sort. - The comparator looks at pairs of elements (a and b), determines their sort order based on the return value. - If it returns a negative number, a should come before b. - If it returns a positive number, b should come before a. - If it returns a 0, a and b are the same as far as the sort is concerned.

Describe how to implement a naive string search algorithm. What is its Big O?

- Loop over the longer string. - Loop over the shorter string. - If the characters don't match, break out of the inner loop. - If the characters do match, keep going. - If you complete the inner loop and find a match, increment the count of matches. - Return the count. Big O: - Best, Avg, Worst: O(NM) of O(N^2)

What are the general rules when deriving space complexity using Big O?

- Most primitives (booleans, numbers, undefined, null) are constant space. - Strings require O(n) space (where n is the string length). - Reference types require O(n) , where n is the length (for arrrays) or the number of keys (for objects).

How do you implement insertion sort?

- Start by picking the second element in the array. - Now compare the second element with the one before it and swap if necessary. Continue to the next element and if it is in the incorrect order, iterate through the sorted portion (the left side) to place the element in the correct place. - Repeat until the array is sorted.

What are the steps for implementing selection sort?

- Store the first element as the smallest value you've seen so far. - Compare this item to the next item in the array until you find a smaller number. - If a smaller number is found, designate that smaller number to be the new minimum and continue until the end of the array. - If the minimum is not the value (index) you initially began with, swap the two values.

Describe how to implement a linear search algorithm. What is its Big O?

- The function accepts an array and a value. - Loop through the array and check if the current element is equal to the value. - If it is, return the index at which the element is found. - If the value is never found, return -1. Big O: - Best: O(1) - Avg: O(N) - Worst: O(N)

Describe how to implement a binary search algorithm. What is its Big O?

- This function accepts a sorted array and a value. - Create a left pointer at the start of the array, and a right pointer at the end of the array. - While the left pointer comes before the right pointer, if you find the value, return index, if the value is too large, move right pointer down. - If you never find the value, return -1. Big O: - Best: O(1) - Avg: O(log n) - Worst: O(log n)

What are binary heaps used for?

- To implement priority queues. - With graph traversal algorithms.

When do you use JS objects?

- When you don't need order. - When you need fast access/insertion and removal.

When do you use arrays?

- When you need order. - When you need fast access/insertion and removal (sort of...).

How does quick sort work?

- Works by selecting one element(called the pivot) and finding the index where the pivot should end up in the sorted array. - Ideally, the pivot should be chosen so that it's roughly the median value in the data set. - Once the pivot is positioned appropriately, quick sort can be applied on either side of the pivot.

How do you improve the speed and distribution of hash functions?

- You can create an upper bound for the loop used to add up character codes. This creates constant time for keys of length higher than the bound. - You can multiply the total by a prime number each loop and then add the character code value before you modulo by the array length. This helps lower collisions because MATH.

What are some use cases for graphs?

- social networks - location/mapping - routing algorithms - visual heirarchy - file system optimizations

What is a tree data structure?

A data structure that consists of nodes in a parent/child relationship.

What is a linked list?

A data structure that contains a head, tail and length property. Linked lists consist of nodes, and each node has a value and a pointer to another node or null.

What is dynamic programming?

A method for solving a complex problem by breaking it down into a collection of simpler subproblems, solving each of those subproblems just once, and storing their solutions.

What is optimal substructure?

A problem has optimal subsructure when an optimal solution can be constructed from optimal solutions of its subproblems.

What is an overlapping subproblem?

A problem that can be broken down into subproblems which are reused several times.

What is a queue?

A queue is a FIFO data structure. It grows from the tail and shrinks from the head. It is unidirectional and spans from head to tail

What is bubble sort? Describe the implementation.

A sorting algorithm where the largest values bubble up to the top. - Loop through the array and compare the first and second elements. - If the first element is greater than the second, swap them. Continue this pattern through the iteration. - Shorten the array length from the tail by 1. Compare elements and swap as needed. Continue this patter until you iterate through the whole array. - For optimization: create a variable called noSwaps, set it to true at the beginning of the top loop. If two elements are swapped, set it to false. When the inner for loop breaks, if noSwaps is set to true, break the outer for loop and return the array.

What is a stack?

A stack is a LIFO data structure similar to a single linked list. It grows and shrinks at the head. It is unidirectional and spans from head to tail.

What is insertion sort?

Builds up the sort by gradually creating a larger left half which is always sorted.

What is space complexity?

How much additional memory we need to allocate in order to run the code in the algorithm.

What is the Big O of stacks & queues?

Insertion - O(1) Deletion - O(1) Searching - O(N) Access - O(N)

What is selection sort?

Places small values into sorted position.

What are queues used for?

Queues are used for task processing, resource uploads, etc...

What are the general rules when deriving time complexity using Big O?

- Arithmetic operations are constant. - Variable assignment is constant. - Accessing elements in an array (by index) or object (by key) is constant. - In a loop, the complexity is the length of the loop times the complexity of whatever happens in side the loop.

What is the logic for the full quick sort algorithm?

- Call the pivot helper on the array. - When the helper returns to you the updated pivot index, recursively call the pivot helper on the subarray to the left of that index, and the subarray to the right of that index. - Your base case occurs when you consider a subarray with less than 2 elements.

What is the logic for merging two sorted arrays?

- Create an empty array, take a look at the smallest values in each input array. - While there are still values we haven't looked at: - If the value in the first array is smaller than the value in the second array, push the value in the first array into our results and move on to the next value in the first array. - else if the its the opposite situation push the value in the second array into our results and move to the next value in the seconds array. - Once we exhaust one array, push all remaining values from the other array.

What is the pseudocode for radix sort?

- Define a function that accepts a list of nums. - Figure out how many digits the largest number has. - Loop from 0 to the largest number of digits. - For each iterations: - Create buckets for each digit (array with 10 subarrays) - place each number in the corresponding bucket based on its kth digit. - Replace our existing arary with values in our buckets, starting with 0 and going up to 9 - Return list at the end.

What are the problems with using time to measure the efficiency of an algorithm?

- Different machines will record different times. - The same machine will record different times! - For fast algorithms, speed measurements may not be precise enough?

What are the constraints of a binary search tree?

- Every parent node has at most, two children. - Every node to the left of a parent node is always less than the parent. - Every node to the right of a parent node is always greater than the parent.

What makes a good hash function?

- Fast (constant time). - Doesn't cluster outputs at specific indices, but distributes uniformly. - Deterministic (same input yields same output).

What is the Big O of hash tables?

- Insert: O(1) - Deletion: O(1) - Access: O(1)

What is a priority queue?

A data structure where each element has a priority. Elements with higher priorities are served before elements with lower priorities.

What is the trade off and benefit when comparing double and single linked lists?

A double linked list offers more flexibility but takes up more memory than a single linked list.

What is a graph?

A graph is a data structure that consists of a finite set of vertices or nodes together with a set of unordered pairs of these vertices for an undirected graph or a set of ordered pairs for a directed graph.

What is the Big O of merge sort?

Big O: - Best, Avg, Worst: O(n log n) - Space: O(n)

What is the Big O for radix sort?

Big O: - Best, Avg, Worst: O(nk) - Space: O(n + k)

What is the Big O complexity of quick sort?

Big O: - Best, Avg: O(n log n) - Worst: O(n^2) - Space: O(log n)

What is the time complexity of bubble & insertion sorts?

Big-O: - Best: O(N) - Avg: O(N^2) -Worst: O(N^2)

What is the time complexity of selection sort?

Big-O: - Best: O(N^2) - Avg: O(N^2) -Worst: O(N^2)

What are some practical applications for a stack data structure?

Function invocations (the call stack), undo/redo, for routing (remember pages you have visited, go forward and back)

What are practical uses for trees?

HTML DOM, network routing, abstract syntax, A.I., folders in OS

What is a hash table?

Hash tables are used to store key-value pairs. They are not ordered. Hash tables are fast for finding values, adding values and removing values.

What is a max binary heap?

In a MaxBinaryHeap the parent nodes are always larger than child nodes. A binary heap is compact as possible. All the children of each node are as full as possible. All the children of each node are as full as they can be and left children are filled out first.

What are the Big O values for: Insertion, Removal, Searching and Access for an array?

Insertion - It depends: - at the end, O(1) - at the beginning, O(N) Removal - It depends: - at the end, O(1) - at the beginning, O(N) Searching - O(N) Access - O(1)

What are the Big O values for: Insertion, Removal, Searching and Access for a JS object?

Insertion - O(1) Removal - O(1) Searching - O(N) Access - O(1)

What is the Big O of BSTs?

Insertion - O(log n) Search - O(log n)

What does Big O notation allow us to do?

It allows us to talk formally about how the runtime of an algorithm grows as the inputs grow.

Compare linked lists and arrays.

Linked List: - Does not have indexes. - Connected via nodes with a next (and prev pointer in doubles) pointer. - Random access is not allowed. Arrays: - Indexed order. - Insertion and deletion can be expensive. - Can quickly be accessed at a specific index.

What is the mathematical operation for counting the digits in a number?

Math.floor(Math.log10(Math.abs(num))) + 1

In which situations do bubble and insertion sort work well?

Nearly sorted data.

What is binary search?

Rather than eliminating one element at a time, you can eliminate half of the remaining elements at a time. Binary search only works on sorted arrays!

Define these terms related to trees: Root, Child, Parent, Siblings, Leaf and Edge.

Root: The top node in a tree. Child: A node directly connected to another node when moving away from the Root. Parent: The converse notion of a child. Siblings: A group of nodes with the same parent. Leaf: A node with no children. Edge: The connection between one node and another.

What are two ways to deal with collisions?

Separate Chaining: at each index our array stores values using a more sophisticated data structure. Linear Probing: when we find a collision, we search through the array to find the next empty slot.

What is tabulation?

Storing the result of a previous result in a table. Usually done using iteration. Better space complexity can be achieved using tabulation.

What is sorting?

The process of rearranging the items in a collection so that the items are in some kind of order.

What is auxiliary space complexity?

The space required by the algorithm, not including space taken up by the inputs. This is what space complexity is talking about in regards to Big-O.

What are the basic terminology involved with graphs?

Vertex - a node. Edge - connection between nodes. Weighted/Unweighted - values assigned to distances between vertices. - Directed/Undirected - directions assigned to distance between vertices.

What are the steps for implementing a push method for single linked list?

Vertex vtx = new Vertex(v) tail.next = vtx tail = vtx

What are the steps for implementing an unshift method for single linked list?

Vertex vtx = new Vertex(v) vtx.next = head head = vtx

What is the definition of Big O?

We say that an algorithm is O(f(n)) if the number of simple operations the computer has to do is eventually less than a constant times f(n), as n increases. Big O references the upper bound, or worst case scenario.

Describe merge sort.

Works by decomposing an array into smaller arrays of 0 or 1 elements, then building up a newly sorted array.

What does the getDigit helper function do for the radix sort algorithm? What is the mathematical operation to do so for base 10 numbers?

getDigit(num, place) - returns the digit in num at given place value. Math.floor(Math.abs(num) / Math.pow(10, i)) % 10;

What are the steps for implementing a pop method for single linked list?

if empty, do nothing Vertex pre = head temp = head.next while (temp.next != null) pre = pre.next pre.next = null delete temp, tail = pre

What are the steps for implementing a shift method for single linked list?

if empty, do nothing temp = head head = head.next delete temp

What are the Big O values for these JS object methods: keys, values, entries, hasOwnProperty?

keys - O(N) values - O(N) entries - O(N) hasOwnProperty - O(1)

What are the Big O values for these array methods: push, pop, shift, unshift, concat, slice, splice, sort, forEach/map/filter/reduce/etc...?

push, pop - O(1) shift, unshift, concat, slice, splice, forEach, map, filter, reduce - O(N) sort - O(N * log N)


संबंधित स्टडी सेट्स

Physical science Big Bang theory

View Set

Wordly Wise 3000 Book 4 Chapter 8

View Set

Chapter 15- Social Psychology in Court

View Set

Правила чтения буквы Kk в английском языке

View Set

chapter 9 : digestive system and nutrition (pt. 3)

View Set