CSE 2050 Exam 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is tabulation?

(Bottom-Up) For Fibonacci: • Solve the small problems first • fill in F[0],F[1] • Then bigger problems • fill in F[2] • Then bigger problems • fill in F[n-1] • Then finally solve the real problem. • fill in F[n]

What is a merge sort?

(Split list in half, sort each half least to greatest) Combine two pre-sorted lists into a sorted whole Merge: Keep track of smallest element in each sorted half. Insert smallest of two elements into temporary array. Repeat until done

What is quickselect?

-Goal is to find the kth smallest element (Ex: if k=3, find the 3rd smallest element) -pick a random element x (called pivot) and partition S into: -L elements less than x , E elements equal x, G elements greater than x -depending on k, either answer is in E, or we need to recur on either L or G

What is a quick sort (naive version)

-Pick some number p (pivot) from the array -Place the pivot in the middle of the array. -In the order they appear in the original list, list the values less than the pivot on the left, and list the values greater than the pivot on the right.

What is a quick sort (in-place version)

-Pick some number p (pivot) from the array -Place the pivot within the middle of the array. -Swap values to get the numbers left of the pivot to be all less than the pivot values and the numbers right of the pivot to be all greater than the pivot. -(probably won't be in full chronological order

What is Memoization?

-Write the recursive function top-down • Alter the function to check if we've already calculated the value • If so, use the pre-calculate value • If not, do the recursive call(s)

Fibonacci numbers using recursion

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 def Fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return Fibonacci(n-1) + Fibonacci(n-2) print(Fibonacci(8)) This will print the 8th value in the fibonacci sequence which is 21

Give the following list after a single in-place run through of `quicksort` (just before the recursive call is made), using the final element as pivot. Idx: 0 1 2 3 4 5 6 7 8 9 L: 63, 81, 42, 86, 91, 73, 64, 53, 41, 58

41 53 42 58, 91, 73, 64, 81, 63, 86 (keep swapping until the values on the left are all less than the pivot)

What is mapping?

A mapping is an association between two sets of things. It associates a value to a key. We refer to these associated pairs as key-value pairs.

Recursion definition

A problem-solving technique that involves breaking a problem into smaller instances of the same problem (also called subproblems) until we get a small enough subproblem having a trivial solution.

What is binary search?

A recursive algorithm where if you are looking for an item in a sorted list, you break the list in half and repeat the search on whichever side could contain the missing element, which can be found by comparing with the median element.

Greedy algorithm definition

An algorithmic strategy that makes the best optimal choice at each small stage with the goal of this eventually leading to a globally optimum solution

We are sorting the list L = [9, 7, 4, 2, 1] with various quadratic sorting algorithms, printing out the value of L after each outer loop. Match the sequence of lists with the correct sorting algorithm: [7, 4, 2, 1, 9] [4, 2, 1, 7, 9] [2, 1, 4, 7, 9] [1, 2, 4, 7, 9] : [7, 9, 4, 2, 1] [4, 7, 9, 2, 1] [2, 4, 7, 9, 1] [1, 2, 4, 7, 9] : [1, 7, 4, 2, 9] [1, 2, 4, 7, 9] [1, 2, 4, 7, 9] [1, 2, 4, 7, 9] : [1, 9, 7, 4, 2] [1, 7, 4, 2, 9] [1, 2, 7, 4, 9] [1, 2, 4, 7, 9] :

Bubble sort Insertion sort Selection sort Cocktail sort

Which algorithms can be optimized to have best case running times of O(n) for certain inputs? Select all that apply

Bubble sort and Insertion sort

Divide and conquer paradigm

Divide-and-conquer paradigm includes three steps: • Divide the problem into subproblems (till a base case) • Conquer the subproblems by solving them recursively • Combine subproblem solutions

Understand the fibonacci recursive flow chart

For Fib(8), 6 and 7 would come from 8 .....

Rehashing

Increases the number of buckets. 𝑏𝑢𝑐𝑘𝑒𝑡 = ℎ𝑎𝑠ℎ 𝑘𝑒𝑦 % 𝑚 Splits keys into 𝑚 buckets • good for ~𝑚 items If 𝑛 gets too big/small, rehash: Create list with 𝑝 buckets • Rehash every key: • 𝑏𝑢𝑐𝑘𝑒𝑡 = ℎ𝑎𝑠ℎ 𝑘𝑒𝑦 % 𝑝 • Cost: O(n) • Average cost: O(1) • Lazy Update!

________ is a Divide and Conquer algorithm. It divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves.

Mergesort

Mergesort - What is the asymptotic upper limit on the number of comparisons when merging two sorted lists of size m and n?

O(m+n)

What is the asymptotic upper limit on the running time (i.e. the big-O) of the following binary search: def bs(L, item): if len(L) == 0: return False median = len(L) // 2 if item == L[median]: return True elif item < L[median]: return bs(L[:median], item) else: return bs(L[median + 1:], item)

O(n)

What is a load factor?

Ratio of number of entries in the table to table size (elements/buckets). If n is the total number of (key, value) pairs stored in the table and c is capacity of the table (i.e., array), the factor is: n/c

What is a bubblesort?

Repeatedly swapping the biggest value with an adjacent spot. (One by One). Continues until the larger elements are bubbled at the end of the array

Which algorithm would be expected to perform the fewest write operations when sorting a randomized list?

Selection sort

Which sorting algorithm does NOT guarantee that any items are in their final position until the final outer loop?

Selection sort

Separate chaining (Open hashing)

Separate chaining is a collision resolution strategy where collisions are resolved by storing all colliding keys in the same slot (using list or some other data structure)

What is insertionsort?

Sort as you go. Items are sorted relative to eachother. (One by one)

What does the load factor decide?

The Load factor is a measure that decides when to increase the HashMap capacity to maintain the get() and put() operation complexity of O(1)

What is hashing?

The information to be retrieved is stored in a hash table which is best thought of as an array of m locations, called buckets -The mapping between a key and a bucket is called the hash function

What is the primary goal of memoization:

To find a more efficient and faster solution

True or False: When dynamic programming is applied to a problem, it takes far less time as compared to other methods that don't take advantage of overlapping subproblems.

True

Compared to recursive solutions, iterative solutions tend to..

Use less memory

Which statement best describes greedy algorithms?

When choosing between multiple paths, they tend to take the path that brings them closest to the solution

What is a hash collision?

When two different keys have the same hash value.

What is a selectionsort?

With every inner loop, select the next highest item and move it to the end.

Memoization visualization

Within the recursive flow chart, we collapse any repeated nodes and do not perform the same work twice

quickselect running time

Worst Case: O(n^2) Average Case: O(n) Best Case: O(n)

quicksort running time

Worst Case: O(n^2) Average Case: O(nlogn) Best Case: O(nlogn)

mergesort running times

Worst Case: O(nlogn) Average Case: O(nlogn) Best Case: O(nlogn)

bubble sort Running Times

Worst case: O(n^2) Average case: O(n^2) Best case: O(n)

insertionsort running times

Worst case: O(n^2) Average case: O(n^2) Best case: O(n)

Selection sort Running Times

Worst case: O(n^2) Average case: O(n^2) Best case: O(n^2)

Recursive function layout

def f(k): (Base Case) if k == 0: return 0 else: (Recursive step) return f(k-1) + k

python hash()

hash() function takes only one parameter. •object (required) - The object whose hash value is to be returned. •Only immutable objects can be hashed.


Ensembles d'études connexes

Reference Section: Computing for Engineers, Computer Science

View Set

Unit Eight - The Department and Insurance Defined

View Set

Розділ 3. УТВЕРДЖЕННЯ КОМУНІСТИЧНОГО ТОТАЛІТАРНОГО РЕЖИМУ В УКРАЇНІ

View Set

Chief Officer 3rd Ed. Ch.2- Professional Development Pg 32-40

View Set

Exam Review BGSU MGMT 3600 CH 1-6

View Set