Test 3 Flashcards
A __________ ___________ is a restricted form of a list. Items have priorities (or _____)
priority queuekeys
A priority queue removes items based on _________
priority
Unsorted list implementation store the items of the priority queue in a ____-_____, in arbitrary order
list-based
Unsorted list implementation example
4 - 5 - 2 - 3 - 1
Unsorted list implementation Performance: ____________ takes _____ time since we can insert the item at the beginning of the sequence
insertItemO(1)
Unsorted list implementation Performance: ___________, __________, and ___________ take ______ time since we have to traverse the entire sequence to find the smallest key
removeItem, min/maxKey, min/maxElementO(n)
Sorted list implementation store the items of the priority queue in a sequence, sorted by _____
key
Sorted list implementation example
1 - 2 - 3 - 4 - 5
Sorted list implementation Performance: __________ takes ______ time since we have to find the place where to insert the item
insertItemO(n)
Sorted list implementation Performance: ___________, __________, and ___________ take ______ time since the smallest key is at the beginning of the sequence
removeItem, min/maxKey, min/maxElementO(1)
Sorting Using a Priority Queue -given a ___________ of N items: a PQ can be used to sort the ___________:
sequencesequence
Sorting Using a Priority Queue -given a sequence of N items: a PQ can be used to sort the sequence: -Step 1: ___________…
Insert N items into the PQ via insertItem(key, data)
Sorting Using a Priority Queue -given a sequence of N items: a PQ can be used to sort the sequence: -Step 1: Insert N items into the PQ via insertItem(key, data) -Step 2: ___________…
Remove the items by calling removeItem() N times-the first remove removes the largest item, the second call the second largest, … the last removes the smallest item
Binary Search -uses a _________ and ___________ methodology
divideconquer
Binary Search -requires data to be ________
sorted
Binary Search -Runs in _______ time
O(lg n)
Binary Search -example of divide and conquer
looking up a word in a dictionary
Bubble Sort 1st run (5), (7), 2, 6, 9, 3 can’t switch 5 and 7 because 7 is larger 5, (7), (2), 6, 9, 3 then 7 and 2 5, 2, (7), (6), 9, 3 switch 7 and 2 because 7 is larger 5, 2, 6, (7), (9), 3 switch 7 and 6 5, 2, 6, 7, (3), (9) switch 9 and 3
the largest value has now bubbled to the end
Bubble Sort 2nd run (5), (2), 6, 7, 3, 9 2, (5), (6), 7, 3, 9 2, 5, (6), (7), 3, 9 2, 5, 6, (7), (3), 9 2, 5, 6, 3, (7), 9
the second largest value has now bubbled to the end
Complexity of bubble sort
n^2
Quicksort Algorithm 1. While data[___________] <= data[______] {++BiggerIndex; }
BiggerIndexpivot
Quicksort Algorithm 2. While data[___________] > data[_____] {–SmallerIndex; }
SmallerIndexpivot
Quicksort Algorithm 3. If ___________ < ____________ { swap data[BiggerIndex] and data[SmallerIndex] }
BiggerIndexSmallerIndex
Quicksort Algorithm 4. While SmallerIndex > BiggerIndex, go to 1
SmallerIndexBiggerIndex
Quicksort Algorithm 5. Swap data[____________] and data[____________]
SmallerIndexpivotIndex
Quicksort Algorithm given an array of n ________ (integers) -if array only contains one element, return -else –pick one element to use as ______ –partition elements into two ___-______: —elements less than or equal to pivot —elements greater than pivot
elementspivotsub-arrays
Quicksort example pivot index = 0 bigger index = 1 smaller index = 8 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 80 | 60 | 50 | 7 | 30 | 100 so is 20 <= 40?
yes
Quicksort example pivot index = 0 bigger index = 2 smaller index = 8 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 80 | 60 | 50 | 7 | 30 | 100 so is 10 <= 40?
yes
Quicksort example pivot index = 0 bigger index = 3 smaller index = 8 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 80 | 60 | 50 | 7 | 30 | 100 so is 80 <= 40?
no
Quicksort example pivot index = 0 bigger index = 3 smaller index = 8 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 80 | 60 | 50 | 7 | 30 | 100 so is 100 > 40?
yes
Quicksort example pivot index = 0 bigger index = 3 smaller index = 7 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 80 | 60 | 50 | 7 | 30 | 100 so is 30 > 40?
no
Quicksort example pivot index = 0 bigger index = 3 smaller index = 7 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 30 | 60 | 50 | 7 | 80 | 100 swap 80 and 30 so is 30 <= 40?
yes
Quicksort example pivot index = 0 bigger index = 4 smaller index = 7 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 30 | 60 | 50 | 7 | 80 | 100 swap 80 and 30 so is 60 <= 40?
no
Quicksort example pivot index = 0 bigger index = 4 smaller index = 7 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 30 | 60 | 50 | 7 | 80 | 100 so is 80 > 40?
yes
Quicksort example pivot index = 0 bigger index = 4 smaller index = 6 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 30 | 60 | 50 | 7 | 80 | 100 so is 7 > 40? so swap 7 and 60
no
Quicksort example pivot index = 0 bigger index = 4 smaller index = 6 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 30 | 7 | 50 | 60 | 80 | 100 so is 7 <= 40?
yes
Quicksort example pivot index = 0 bigger index = 5 smaller index = 6 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 30 | 7 | 50 | 60 | 80 | 100 so is 50 <= 40?
no
Quicksort example pivot index = 0 bigger index = 5 smaller index = 6 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 40 | 20 | 10 | 30 | 7 | 50 | 60 | 80 | 100 so is 60 > 40? then the bigger index = smaller index
yes
Quicksort example pivot index = 0 bigger index = 5 smaller index = 4 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 7 | 20 | 10 | 30 | 40 | 50 | 60 | 80 | 100 swap 40 and 7 so what’s the new pivot index?
pivotIndex = 4
Quicksort Analysis with the assumption the keys are random uniformly distributed, the runtime of QuickSort is usually taken to be ____________
O(n lg n)
Quicksort to avoid such bad scenarios a better choice of pivot is typically used: use the Median of 3 Rule:
pick the median value of 3 elements from the data array as the pivot specifically: data[0], data[n/2], and data[n-1]
Quicksort median of 3 rule example: data[0] = 10, data[n/2] = 70, data[n-1] = 50 median of {10, 50, 70} = ?
50
Quicksort: small arrays for very small arrays (circa n <= 20), quicksort in physical time does not perform as well as __________ ______.
insertion sort
Quicksort: small arrays And because quicksort is ___________, these cases occur frequently
recursive
“Quicksort: small arrays common solution: when the array size becomes ““sufficiently”” small _____ ______ ______ quicksort recursively. instead, use a sorting algorithm that is more efficient for small arrays (such as ___________ _____)”
do not useinsertion sort
worst case time for Selection Sort
O(n^2)
worst case time for Insertion Sort
O(n^2)
worst case time for Bubble Sort
O(n^2)
worst case time for Heap Sort
O(n log(n) )
worst case time for Merge Sort
O(n log(n) )
worst case time for Quick Sort
O(n^2)
________ sort is based on divide and conquer paradigm
merge
Merge sort consists of three steps. What is the first step?
Divide: partition input sequence S into two sequences S1 and S2 of about n/2 elements each
Merge sort consists of three steps. What is the second step?
Recur: recursively sort S1 and S2
Merge sort consists of three steps. What is the third step?
Conquer: merge S1 and S2 into a unique sorted sequence
MergeSort you may also see MergeSort Trees that: -instead of reversing direction back up the tree -continue down – ______________…
merging the branches as they go
MergeSort the execution results of a MergeSort can be represented as a _________ _______
binary tree
MergeSort uses a ________ and ________ method
divide conquer
MergeSort The height of the tree is _________, where N is the number of items being sorted
lg N
MergeSort The height of the tree is lg N, where N is the number of items being sorted How do we know that? because lg N is _________________….
the number of times N can be divided by 2 (integer wise)
Runtime of MergeSort knowing the height of the representative tree is lg N is important because:
“it is the ““key concept”” in showing/proving the runtime of MergeSort is O(N lg N)”
“Merge sort consists of three steps. The first step ““divide”” has what runtime?”
O(1)