Divide and Conquer Merge Sort Partitioning numbers Quick
Divide and Conquer • Merge Sort • Partitioning numbers • Quick Sort A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 1
Partitioning Numbers • We have a set of numbers in an array. The goal is to pick up a pivot number and divide the array into two sections: • The left of the pivot should include numbers smaller than the pivot and • The right of the pivot should include numbers bigger than the pivot • For example we have the following array: 17 21 5 23 9 37 15 3 11 25 31 13 29 7 19 • Assuming that we choose the pivot to be 17, we would like to partition the array to get something like the following arrangement: 7 5 9 15 3 11 13 17 37 25 31 23 29 21 19 • Note that every number less than 17 is to the left of 17 and every number bigger than 17 is to the right of 17. A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 2
Partitioning Algorithm Partition (a, i, j) { val = a[i]; // val is pivot number we choose h = i; for (k=i+1 to j) if (a[k] <= val) { h++; Swap(a[h], a[k]); } Swap (a[i], a[h]) return h; } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 3
• Example: • Suppse we want to partition the following array with pivot to be 17 • The value of i is 0, the beginning of the array and the value of j is 13 that is the index of the last element of the array 17 21 5 23 9 37 15 3 11 25 31 13 29 7 19 i h k • H starts from I and k iterated through the array until it reaches the end of the array. A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 4
17 21 5 i h 23 9 37 15 3 11 25 31 13 29 7 19 k • As k iterates, it finds the next element that the smaller than 17 (the pivot). • 5 is the first smallest number. Now • H is incremented by one and 17 21 5 i h k 23 9 37 15 3 11 25 31 13 29 7 19 • The values a[k] and a[h] are swapped: 17 5 21 i h k A. R. Hadaegh Dr. Ahmad R. Hadaegh 23 9 37 15 3 11 25 31 California State University San Marcos (CSUSM) 13 29 7 19 Page 5
17 5 21 i h k 23 9 37 15 3 11 25 31 13 29 7 19 • As k iterates, it finds the next element that the smaller than 17 (the pivot). 9 is the next smallest number. Now • h is incremented by one and 17 5 21 23 h i 9 37 15 3 11 25 31 13 29 7 19 k • The values a[k] and a[h] are swapped: 17 5 i A. R. Hadaegh Dr. Ahmad R. Hadaegh 9 h 23 21 37 15 3 11 25 31 13 29 7 19 k California State University San Marcos (CSUSM) Page 6
17 5 9 23 21 15 3 11 25 31 13 29 7 19 k h i 37 • The next element smaller than 17 is 15 • h is incremented by one and 17 5 9 23 21 37 h i 15 3 11 k • The values a[k] and a[h] are swapped: 17 i A. R. Hadaegh Dr. Ahmad R. Hadaegh 5 9 15 h 21 37 23 3 11 25 31 13 29 7 19 k California State University San Marcos (CSUSM) Page
17 5 9 15 21 37 h i 23 3 11 25 31 13 29 7 19 k • The next element smaller than 17 is 3 • h is incremented by one and 17 5 9 15 21 37 23 11 25 31 13 29 7 19 k h i 3 • The values a[k] and a[h] are swapped: 17 5 i A. R. Hadaegh Dr. Ahmad R. Hadaegh 9 15 3 h 37 23 21 11 k California State University San Marcos (CSUSM) Page
17 5 9 15 3 37 23 11 25 31 13 29 7 19 k h i 21 • The next element smaller than 17 is 11 • h is incremented by one and 17 5 9 15 3 37 23 21 h i 11 25 31 13 29 7 19 k • The values a[k] and a[h] are swapped: 17 5 i A. R. Hadaegh Dr. Ahmad R. Hadaegh 9 15 3 11 h 23 21 37 25 31 13 29 7 19 k California State University San Marcos (CSUSM) Page
17 5 9 15 3 11 23 21 37 25 31 13 h i 29 7 19 k • The next element smaller than 17 is 13 • h is incremented by one and 17 5 9 15 3 11 i 23 21 37 25 31 h 13 k • The values a[k] and a[h] are swapped: 17 5 i A. R. Hadaegh Dr. Ahmad R. Hadaegh 9 15 3 11 13 21 37 25 31 h California State University San Marcos (CSUSM) 23 29 7 19 k Page
17 5 9 15 3 11 i 13 21 37 25 31 h 23 29 7 19 k • The next element smaller than 17 is 7 • h is incremented by one and 17 5 9 15 3 11 13 i 21 37 25 31 23 29 h 7 19 k • The values a[k] and a[h] are swapped: 17 5 i A. R. Hadaegh Dr. Ahmad R. Hadaegh 9 15 3 11 13 7 37 25 31 23 h California State University San Marcos (CSUSM) 29 21 19 k Page
17 5 9 15 3 11 13 i 7 37 25 31 23 29 h 21 19 k • We are almost done. The last step is to swap a[h] and a[i] • The final result is: 7 5 9 15 3 11 13 i 17 37 25 31 23 29 h 21 19 k • Note that tall numbers smaller than 17 are now on the left of 17 and all numbers greater than 17 are to the right of 17. 7 5 A. R. Hadaegh Dr. Ahmad R. Hadaegh 9 15 3 11 13 17 37 25 31 23 California State University San Marcos (CSUSM) 29 21 19 Page
Quick Sort Algorithm Quick. Sort(a, i, j) { If (i < j) { P = partition (a, i, j); Quick. Sort(a, i, p); Quick. Sort(a, p, j) } } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Example: • Sort the following numbers using quick sort algorithm. 17 21 5 23 9 37 15 3 11 25 31 13 29 7 19 • Using 17 as pivot and partitioning the elements we get 7 5 9 15 3 11 13 17 37 25 31 23 29 21 19 • Now we partition the elements to the left of 17 • And then partition the elements to the right of 17 A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 14
7 5 9 15 3 11 13 17 37 25 31 23 29 21 19 • To partition the elements to the left of 17, we use 7 as pivot and we get: 3 5 7 15 9 11 13 17 37 25 31 23 29 21 19 • To partition the elements to the right of 17, we use 37 as pivot and we get 3 5 7 A. R. Hadaegh Dr. Ahmad R. Hadaegh 15 9 11 13 17 19 25 31 23 California State University San Marcos (CSUSM) 29 21 37 Page 15
3 5 7 15 9 11 13 17 19 25 31 23 29 21 37 • Now we need to partition the elements to the left of 7, to the right of 7 and to the left of 37. • To partition the elements to the left of 7, we choose 3 as pivot. • To partition the elements to the right of 7, we choose 15 as pivot • To partition elements to the left of 37, we choose 19 as pivot • and we get 3 5 7 A. R. Hadaegh Dr. Ahmad R. Hadaegh 13 9 11 15 17 19 25 31 California State University San Marcos (CSUSM) 23 29 21 37 Page 16
3 5 7 13 9 11 15 17 19 25 31 23 29 21 37 • Now we need to partition the elements to the right of 3, to the left of 15, and to the right of 19 • To partition the elements to the right of 3, we choose 5 as pivot. • To partition the elements to the left of 15, we choose 13 as pivot • To partition elements to the right of 19, we choose 25 as pivot • and we get 3 5 7 A. R. Hadaegh Dr. Ahmad R. Hadaegh 11 9 13 15 17 19 21 23 California State University San Marcos (CSUSM) 25 29 31 37 Page 17
3 5 7 11 9 13 15 17 19 21 23 25 29 31 37 • Now we need to partition the elements to the left of 13, to the left of 25 and to the right of 25 • To partition the elements to the left of 13, we choose 11 as pivot • To partition the elements to the left of 25, we choose 21 as pivot • To partition elements to the right of 25, we choose 29 as pivot • and we get 3 5 7 9 11 13 15 17 19 21 23 25 29 31 37 • The elements are now sorted A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 18
• Now we need to partition the elements to the left of 13, to the left of 25 and to the right of 25 • To partition the elements to the left of 13, we choose 11 as pivot • To partition the elements to the left of 25, we choose 21 as pivot • To partition elements to the right of 25, we choose 29 as pivot • and we get 3 5 7 9 11 13 15 17 19 21 23 25 29 31 37 • The elements are now sorted A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 19
17 7 3 5 5 21 5 9 7 23 9 37 15 3 11 13 15 9 11 13 3 5 7 13 9 3 5 7 11 9 13 3 5 7 9 11 13 A. R. Hadaegh Dr. Ahmad R. Hadaegh 11 15 3 11 17 17 25 31 13 37 25 31 19 25 31 17 19 15 17 19 21 15 25 29 7 19 23 29 21 19 37 23 29 21 37 23 25 29 31 California State University San Marcos (CSUSM) 31 37 Page 20
Recursive Binary Search • Bisect the current range first. . . last at mid • if A[mid] == key we are done • if A[ mid] < key then • key is in upper half of region first. . . Last • Continue by searching upper half mid+1. . . Last • if key < A[mid] then • key is in lower half of array if it is in array at all • continue by searching lower half first. . . mid- 1 A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Recursive Binary Search • This is a recursive description algorithm defined in terms of itself • if (first > last) then • we are finished • key is not found • otherwise, • compute mid and compare A[mid] to key • recursively search lower or upper half accordingly • return result of search A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Recursive Binary Search int binsearch (const vector< int>& A, int key int first, int last) { if (first> last) return -1; // key not found (null vector) else { int mid = (first+ last)/ 2; // define mid- point if (A[mid] == key) return mid; // found it if (A[mid] < key) // search upper half of region return binsearch( A, key, mid+1, last); else // search lower half of region return binsearch( A, key, first, mid-1); } } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Recursive Binary Search - Consider the sorted array index: 0 1 2 3 4 5 6 7 value: 2 3 5 7 11 13 14 17 – Start by calling binsearch( A, 11, 0, 7) - mid = 3 and A[3] < 11 // key in A[ 4]. . . A[ 7] – call binsearch( A, 11, 4, 7) - mid = 5 and A[5] > 11 // key in A[ 4]. . A[ 4] – call binsearch( A, 11, 4, 4) - mid = 4 and A[4]==11 - Found it A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Mergesort • An important example of divide and conquer algorithm is mergesort • It is known as one of the fastest known "comparison based“ sorting algorithms • It is also extremely fast in practice A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Mergesort • Main idea: • break the array into two unsorted arrays of equal size • sort each side recursively • merge the two sorted halves into a single sorted array A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Mergesort • For example, on array A = { 5, 9, 3, 13, 2, 6, 4, 1, 3, 7 } left = { 5, 9, 3, 13, 2 } right = { 6, 4, 1, 3, 7 } • Recursively sort left to {2, 3, 5, 9, 13} • Recursively sort right to {1, 3, 4, 6, 7} • Merge the two sorted arrays into a single sorted array: { 1, 2, 3, 3, 4, 5, 6, 7, 9, 13 } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Mergesort: merging • Aside from the recursion, the only operation which needs to be accomplished is to merge two sorted lists • Idea: • maintain a "pointer" into each sorted array • at each step, append the smallest element pointed to onto the final array A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Mergesort: merging Step 1: sort_ left = { 2 , 3, 5, 9, 13 } sorted_ right = { 1 , 3, 4, 6, 7 } sorted_ array = {} Step 2: sort_ left = { 2 , 3, 5, 9, 13 } sorted_ right = { 1, 3 , 4, 6, 7 } sorted_ array = { 1 } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Step 3: sort_ left = { 2, 3 , 5, 9, 13 } sorted_ right = { 1, 3 , 4, 6, 7 } sorted_ array = { 1, 2 } Step 4: sort_ left = { 2, 3, 5 , 9, 13 } sorted_ right = { 1, 3 , 4, 6, 7 } sorted_ array = { 1, 2, 3 } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Step 5: sort_ left = { 2, 3, 5 , 9, 13 } sorted_ right = { 1, 3, 4 , 6, 7 } sorted_ array = { 1, 2, 3, 3 } Step 6: sort_ left = { 2, 3, 5 , 9, 13 } sorted_ right = { 1, 3, 4, 6 , 7 } sorted_ array = { 1, 2, 3, 3, 4 } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Step 7: sort_ left = { 2, 3, 5, 9 , 13 } sorted_ right = { 1, 3, 4, 6 , 7 } sorted_ array = { 1, 2, 3, 3, 4, 5 } Step 8: sort_ left = { 2, 3, 5, 9 , 13 } sorted_ right = { 1, 3, 4, 6, 7 } sorted_ array = { 1, 2, 3, 3, 4, 5, 6 } Step 9: sort_ left = { 2, 3, 5, 9 , 13 } sorted_ right = { 1, 3, 4, 6, 7 } sorted_ array = { 1, 2, 3, 3, 4, 5, 6, 7 } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Step 10: sort_ left = { 2, 3, 5, 9, 13 } sorted_ right = { 1, 3, 4, 6, 7 } sorted_ array = { 1, 2, 3, 3, 4, 5, 6, 7, 9 } Step 11: sort_ left = { 2, 3, 5, 9, 13 } sorted_ right = { 1, 3, 4, 6, 7 } sorted_ array = { 1, 2, 3, 3, 4, 5, 6, 7, 9, 13 } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Mergesort: merging vector< int> merge( const vector< int>& L, const vector< int>& R) { int Lptr= 0, Rptr= 0; vector< int> A; while (Lptr< L. size() || Rptr< R. size()) { if ((Lptr< L. size()) && ((Rptr>= R. size() || L[Lptr]<= R[Rptr]))) { A. push_back( L[Lptr]); Lptr++; } else { A. push_back(R[Rptr]); Rptr++; } } return A; } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
Mergesort • The recursive mergesort routine itself is very simple vector<int> mergesort (const vector<int>& A) { vector< int> left, right; if (A. size() <= 1) return A; for (int i= 0; i< A. size()/ 2; i++) left. push_back(A[ i]); for (i=A. size()/ 2; i< A. size(); i++) right. push_back(A[ i]); return merge(mergesort( left), mergesort( right)); } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
• Again, follow the recursion • We split the array into two • And each half into two recursively • And so on - eventually it gets split into N lists of a single element each A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
• For each split at the end, a pair of elements is merged • Then we merge the pairs, and then the quartets • And so on until one merge of two large lists at the end • This is a by far faster than some other sorts like bubble sort or insertion sort algorithm A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page
- Slides: 38