Download presentation
Presentation is loading. Please wait.
Published byMarian Summers Modified over 8 years ago
1
CSE 250 – Data Structures
2
Today’s Goals First review the easy, simple sorting algorithms Compare while inserting value into place in the vector Go through unsorted data & select value to be added Can sorting get faster? If so, are there limits? What does this mean for sorting & first sorts Divide-and-conquer approaches are faster how? Use execution trees to show how merge sort works Learn quick sort weakness and how it can be avoided
3
Simple Sorting Algorithms Splits vector into ordered & unordered parts Grows ordered part by 1 until entire vector sorted Way in which values added depends on the sort: Insert Insert 1 st unordered value by shifting larger ordered values select From unordered values, select smallest to add at end
4
Simple Sorting Algorithms Splits vector into ordered & unordered parts Grows ordered part by 1 until entire vector sorted Way in which values added depends on the sort: Insert Insert 1 st unordered value by shifting larger ordered values select From unordered values, select smallest to add at end What is time complexity for each of these?
5
Simple Sorting Algorithms Splits vector into ordered & unordered parts Grows ordered part by 1 until entire vector sorted Way in which values added depends on the sort: Insert Insert 1 st unordered value by shifting larger ordered values select From unordered values, select smallest to add at end What is time complexity for each of these? O(n)
6
Simple Sorting Algorithms Splits vector into ordered & unordered parts Grows ordered part by 1 until entire vector sorted Way in which values added depends on the sort: Insert Insert 1 st unordered value by shifting larger ordered values select From unordered values, select smallest to add at end What is time complexity for each of these? O(n)
7
Simple Sorting Algorithms Splits vector into ordered & unordered parts Grows ordered part by 1 until entire vector sorted Way in which values added depends on the sort: Insert Insert 1 st unordered value by shifting larger ordered values select From unordered values, select smallest to add at end What is time complexity for each of these? Complexity usually multiplied since actions are nested O(n)
8
Simple Sorting Algorithms Splits vector into ordered & unordered parts Grows ordered part by 1 until entire vector sorted Way in which values added depends on the sort: Insert Insert 1 st unordered value by shifting larger ordered values select From unordered values, select smallest to add at end What is time complexity for each of these? Complexity usually multiplied since actions are nested O(n) * O(n) = O(n 2 ) O(n)
9
Can You Guess My Name? void __________(vector &vec) { for (int i=0; i < vec.size()-1; i++){ int j = i; for (int k=i+1; k < vec.size(); k++){ if (vec[k] < vec[j]) { j=k; } } swap(vec[i], vec[j]); } }
10
Can You Guess My Name? void __________(vector &vec) { for (int i=0; i < vec.size()-1; i++){ int j = i; for (int k=i+1; k < vec.size(); k++){ if (vec[k] < vec[j]) { j=k; } } swap(vec[i], vec[j]); } }
11
Can You Guess My Name? void __________(vector &vec) { for (int i=0; i < vec.size()-1; i++){ int j = i; for (int k=i+1; k < vec.size(); k++){ if (vec[k] < vec[j]) { j=k; } } swap(vec[i], vec[j]); } } /* Loops through vector 1 value at a time, but both sorts do this! */
12
Can You Guess My Name? void __________(vector &vec) { for (int i=0; i < vec.size()-1; i++){ int j = i; for (int k=i+1; k < vec.size(); k++){ if (vec[k] < vec[j]) { j=k; } } swap(vec[i], vec[j]); } }
13
Can You Guess My Name? void __________(vector &vec) { for (int i=0; i < vec.size()-1; i++){ int j = i; for (int k=i+1; k < vec.size(); k++){ if (vec[k] < vec[j]) { j=k; } } swap(vec[i], vec[j]); } } /* Selecting smallest value! */
14
Can You Guess My Name? void selectionSort(vector &vec) { for (int i=0; i < vec.size()-1; i++){ int j = i; for (int k=i+1; k < vec.size(); k++){ if (vec[k] < vec[j]) { j=k; } } swap(vec[i], vec[j]); } }
15
Insertion Sort void insertionSort(vector &vec) { for (int i=0; i 0 && tmp < vec[j-1]; j--){ vec[j] = vec[j-1]; } vec[j] = tmp; } }
16
Selection v. Insertion Sort Selection SortInsertion Sort Selects smallest remaining Ordered portion complete Number shifts always O(n) O(n 2 ) time complexity In-memory -- O(1) space Selects next unordered value Not complete until end Up to O(n 2 ) shifts needed O(n 2 ) time complexity In-memory -- O(1) space
17
Counting Comparisons decision tree Consider sort as a path in a decision tree Nodes are single decision needed for sorting yesno Is x i > x j ?
18
Counting Comparisons decision tree Consider sort as a path in a decision tree Nodes are single decision needed for sorting Traveling from root to leaf sorts data Tree’s height is lower-bound on sorting complexity
19
Decision Tree Height Leaf needed for each order values could appear Needed to ensure we sort different inputs differently Consider 4, 5 as data to be sorted using a tree Could be entered in 2 possible orders: 4, 5 or 5, 4 Need two leaves for this sort unless (4 < 5) == (5 < 4)
20
Decision Tree Height Leaf needed for each order values could appear Needed to ensure we sort different inputs differently Consider 4, 5 as data to be sorted using a tree Could be entered in 2 possible orders: 4, 5 or 5, 4 Need two leaves for this sort unless (4 < 5) == (5 < 4) For sequence of n numbers, can arrange n! ways Tree with n! leaves needed to sort n numbers Given this many leaves, what is height of the tree?
21
Decision Tree Height With n ! external nodes, height would be:
22
The Lower Bound But what does O(log(n !) ) equal? n ! = n * n -1 * n -2 * n -3 * n /2 * … * 2 * 1 n ! ≤ ( ½* n ) ½* n (½ of series is larger than ½* n ) log(n!) ≤ log((½*n) ½*n ) log(n!) ≤ ½*n * log(½*n) O(log(n!)) ≤ O(½*n * log(½*n))
23
The Lower Bound But what does O(log(n !) ) equal? n ! = n * n -1 * n -2 * n -3 * n /2 * … * 2 * 1 n ! ≤ ( ½* n ) ½* n (½ of series is larger than ½* n ) log(n!) ≤ log((½*n) ½*n ) log(n!) ≤ ½*n * log(½*n) O(log(n!)) ≤ O(½*n * log(½*n))
24
The Lower Bound But what does O(log(n !) ) equal? n ! = n * n -1 * n -2 * n -3 * n /2 * … * 2 * 1 n ! ≤ ( ½* n ) ½* n (½ of series is larger than ½* n ) log(n!) ≤ log((½*n) ½*n ) log(n!) ≤ ½*n * log(½*n) O(log(n!)) ≤ O(n log n)
25
Lower Bound on Sorting Smallest number of comparisons is tree’s height Decision tree sorting n elements has n! leaves At least log( n !) height needed for this many leaves This simplifies to at most O(n log n) height O(n log n) time needed to compare data! Can we define sort that runs in minimum time?
26
Julius, Seize Her! Formula to Roman success Divide peoples before an attack Then conquer weakened armies Common programming paradigm Divide: split into 2 partitions Recur : solve for partitions Conquer: combine solutions
27
Divide-and-Conquer Like all recursive algorithms, need base case Has immediate solution to a simple problem Work is not easy and sorting 2+ items takes work 1 item sorted since it cannot be out of order Sorting a vector with 0 items even easer Recursive step simplifies problem & combines it Begins by splitting data into two equal vector s Merges sub vector s after they have been sorted
28
Execution Tree Depicts divide-and-conquer execution Recursive call represented by each oval node Original vector shown at start At the end of the oval, sorted vector shown Initial call at root of the (binary) tree Bottom of the tree has leaves for base cases
29
Execution Tree Depicts divide-and-conquer execution Recursive call represented by each oval node Original vector shown at start At the end of the oval, sorted vector shown Initial call at root of the (binary) tree Bottom of the tree has leaves for base cases
30
Merge Sort Execution Not in a base case 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9
31
Merge Sort Execution Not in a base case, so split into lefty & righty 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9
32
Merge Sort Execution Not in a base case, so split into lefty & righty 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9
33
Merge Sort Execution Recursively call merge-sort on lefty 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9
34
Merge Sort Execution Recursively call merge-sort on lefty 7 2 9 4 2 4 7 9 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9
35
Merge Sort Execution Not in a base case, so split into lefty & righty (again) 7 2 9 4 2 4 7 9 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9
36
Merge Sort Execution Not in a base case, so split into lefty & righty (again) 7 2 9 4 2 4 7 9 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9
37
Merge Sort Execution Recursively call merge-sort on lefty 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9
38
Merge Sort Execution Recursively call merge-sort on lefty 7 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9
39
Merge Sort Execution Still no base case, so split again & recurse on lefty 7 7 7 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9
40
Merge Sort Execution Enjoy the base case – literally no work to do! 7 77 7 7 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9
41
7 77 7 Merge Sort Execution Recurse on righty and solve for this base case 2 22 2 7 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9
42
Merge Sort Execution Merge the two solutions to complete this call 7 77 72 22 2 7 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9
43
Merge Sort Execution Recurse on righty and sort this vector 9 4 4 9 7 77 72 22 2 7 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9
44
Merge Sort Execution Split into lefty & righty and solve the base cases 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
45
Merge Sort Execution Merge the 2 solutions to sort this vector 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
46
Merge Sort Execution Merge the 2 solutions to sort this vector 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
47
Merge Sort Execution Merge the 2 solutions to sort this vector 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
48
Merge Sort Execution Merge the 2 solutions to sort this vector 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
49
Merge Sort Execution Merge the 2 solutions to sort this vector 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
50
Merge Sort Execution Merge the 2 solutions to sort this vector 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
51
Merge Sort Execution I feel an urge, an urge to merge 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
52
Merge Sort Execution Let's do the merge sort again! (with righty ) 3 8 6 1 1 3 6 8 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
53
Merge Sort Execution Let's do the merge sort again! (with righty ) 3 8 3 8 8 88 83 33 3 3 8 6 1 1 3 6 8 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
54
Merge Sort Execution Let's do the merge sort again! (with righty ) 6 1 1 6 6 66 61 11 1 3 8 3 8 8 88 83 33 39 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2 3 8 6 1 1 3 6 8
55
Merge Sort Execution Let's do the merge sort again! (with righty ) 6 1 1 6 6 66 61 11 1 3 8 3 8 8 88 83 33 3 3 8 6 1 1 3 6 8 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
56
Merge Sort Execution Merge the last call to get the final result 6 1 1 6 6 66 61 11 1 3 8 3 8 8 88 83 33 3 3 8 6 1 1 3 6 8 9 94 44 4 9 4 4 97 2 2 7 7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 7 77 72 22 2
57
Quick Sort Divide: Partition by pivot L has values <= p G uses values >= p Recur: Sort L and G Conquer: Merge L, p, G p
58
Quick Sort Divide: Partition by pivot L has values <= p G uses values >= p Recur: Sort L and G Conquer: Merge L, p, G p
59
Quick Sort Divide: Partition by pivot L has values <= p G uses values >= p Recur: Sort L and G Conquer: Merge L, p, G p L G p
60
Quick Sort Divide: Partition by pivot L has values <= p G uses values >= p Recur: Sort L and G Conquer: Merge L, p, G p p L G p
61
Execution Example pivot Each call starts by selecting the pivot 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
62
Execution Example pivot Each call starts by selecting the pivot 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
63
Execution Example pivot Split into L & G partitions around pivot 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
64
Execution Example pivot Split into L & G partitions around pivot 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
65
Execution Example Recursively solve L partition first 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
66
Execution Example Recursively solve L partition first 2 4 3 1 1 2 3 4 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
67
Execution Example pivot Select new pivot and split into L & G partitions 2 2 4 3 1 1 2 3 4 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
68
Execution Example Recursively solve for L 1 1 2 2 4 3 1 1 2 3 4 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
69
Execution Example Recursively solve for L & enjoy base case 1 11 1 2 2 4 3 1 1 2 3 4 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
70
Execution Example Now solve for G partition and select pivot 3 4 3 3 41 11 1 2 2 4 3 1 1 2 3 4 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
71
Execution Example Recursively solve for L 3 4 3 3 4 1 11 1 2 2 4 3 1 1 2 3 4 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
72
Execution Example Recursively solve for L & enjoy base case 3 4 3 3 4 1 11 1 2 2 4 3 1 1 2 3 4 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
73
Execution Example Now solve for G & enjoy base case 4 44 4 3 4 3 3 4 1 11 1 2 2 4 3 1 1 2 3 4 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9
74
Execution Example pivot Add L, pivot, & G to complete previous call 33 4 3 3 4 1 11 1 2 2 4 3 1 1 2 3 4 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 4 44 4
75
Execution Example pivot Add L, pivot, & G to complete previous call 1 11 1 22 2 4 3 1 1 2 3 4 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 4 44 4 33 4 3 3 4
76
Execution Example Recursively sort G from original call 1 11 1 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 4 44 4 22 2 4 3 1 1 2 3 4 33 4 3 3 4
77
Execution Example Recursively sort G from original call 1 11 1 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 4 44 4 7 9 7 7 7 9 22 2 4 3 1 1 2 3 4 33 4 3 3 4
78
Execution Example pivot Select pivot & partition into L & G 1 11 1 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 4 44 4 7 7 9 7 7 7 9 22 2 4 3 1 1 2 3 4 33 4 3 3 4
79
Execution Example Solve L recursively via base case & move to G 1 11 1 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 4 44 4 7 7 9 7 7 7 9 7 9 7 9 22 2 4 3 1 1 2 3 4 33 4 3 3 4
80
Execution Example pivot Select pivot & partition into L & G 1 11 1 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 4 44 4 7 7 9 7 7 7 9 7 7 9 7 9 22 2 4 3 1 1 2 3 4 33 4 3 3 4
81
Execution Example Solve through two base cases in L & G 9 99 9 1 11 1 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 4 44 4 7 7 9 7 7 7 9 7 7 9 7 9 22 2 4 3 1 1 2 3 4 33 4 3 3 4
82
Execution Example pivot Add L, pivot, & G to complete the call 9 99 9 1 11 1 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 4 44 4 7 7 9 7 7 7 9 77 7 9 7 9 22 2 4 3 1 1 2 3 4 33 4 3 3 4
83
Execution Example pivot Add L, pivot, & G to complete the call 9 99 9 1 11 1 6 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 4 44 4 77 7 9 7 7 7 9 77 7 9 7 9 22 2 4 3 1 1 2 3 4 33 4 3 3 4
84
Execution Example pivot Add L, pivot, & G to complete final call 66 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 9 99 9 1 11 1 4 44 4 77 7 9 7 7 7 9 77 7 9 7 9 22 2 4 3 1 1 2 3 4 33 4 3 3 4
85
Execution Example only as good as pivot choice Sorts data, but only as good as pivot choice 66 7 2 9 4 3 7 6 1 1 2 3 4 6 7 7 9 9 99 9 1 11 1 4 44 4 77 7 9 7 7 7 9 77 7 9 7 9 22 2 4 3 1 1 2 3 4 33 4 3 3 4
86
Pivot Choice Ideas Choose first (or last) value in part of vector Might work, unless vector already sorted
87
Pivot Choice Ideas Choose first (or last) value in part of vector & often is Might work, unless vector already sorted & often is
88
Pivot Choice Ideas Choose first (or last) value in part of vector Might work, unless vector already sorted & often is People like order, so this is often worst choice
89
Pivot Choice Ideas Choose first (or last) value in part of vector Might work, unless vector already sorted & often is People like order, so this is often worst choice Could instead choose random value in partition
90
Pivot Choice Ideas Choose first (or last) value in part of vector Might work, unless vector already sorted & often is People like order, so this is often worst choice Could instead choose random value in partition
91
Pivot Choice Ideas Choose first (or last) value in part of vector Might work, unless vector already sorted & often is People like order, so this is often worst choice Could instead choose random value in partition Worst case much less likely, but still not impossible Best could be median of first, last, & middle values Very unlikely for worst case running time to occur But also adds much more complexity to your code
92
void ________(vector &vec) { if (vec.size() fst(vec.begin(),vec.begin()+n/2); vector snd(vec.begin()+n/2, vec.end()); ________(fst); ________(snd); conquer(vec, fst, snd); }
93
It’s Merge Sort! void mergeSort(vector &vec) { // Check for base case if (vec.size() fst(vec.begin(),vec.begin()+n/2); vector snd(vec.begin()+n/2, vec.end()); // Recursively solve smaller problems mergeSort(fst); mergeSort(snd); // Conquer by merging solutions merge (vec, fst, snd); }
94
Quick Sort v. Merge Sort Quick SortMerge Sort Work mostly splitting data Cannot guarantee even split Should skip some comparisons Does not need extra space Less work allocating arrays Blindly merges all the data Data already in sorted order! Blindly splits data in half Always gets even split Needs * to use other arrays Wastes time in allocation Complex workaround exists Work mostly in merge step Combines two (sorted) halves Always skips some comparisons
95
Limit to Code Want to write sort once – extra work never good So far this lecture, sorts had int type hardcoded template declares generic types for a function Command for works above function using generics template Sort reuse limited by for comparisons Instead add comparison function as a parameter * before function name otherwise listed like function
96
Fully Generic _______ Sort template void _____ (vector &vec, int (*cmp)(GenType, GenType)) { for (int i=1; i 0 && cmp(tmp, vec[j]) < 0) { vec[j] = vec[j-1]; j -= 1; } vec[j] = tmp; } }
97
For Next Lecture Homework #1 due tonight Deadline is very soon -- must be submitted by 2AM Next week’s lectures on Sets and Maps
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.