180 likes | 258 Views
Learn about the divide and conquer algorithm design strategy, examples like mergesort and quicksort, their complexity, and implementation details. Understand Master Theorem and practice coding with two popular algorithms.
E N D
The Project for the Establishing the Korea ㅡ Vietnam College of Technology in BacGiang Algorithms April-May 2013 Dr. Youn-Hee Han
Divide & Conquer • The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smallerinstances 2. Solve smaller instances recursively 3. Obtain solution to original (larger) instance by combining these solutions - “Combining these solutions” is optional
Divide & Conquer a problem of size n (instance) subproblem 1 of size n/2 subproblem 2 of size n/2 a solution to subproblem 1 a solution to subproblem 2 a solution to the original problem It general leads to a recursive algorithm!
Divide & Conquer • Examples • Sorting: mergesort and quicksort • Binary tree traversals • Binary search • Multiplication of large integers • Matrix multiplication: Strassen’s algorithm
MergeSort • Algorithm Summary – Part1 • Split array A[0..n-1] into about equal halves and make copies of each half in arrays B and C • MergeSortarrays B and C recursively • Merge the sorted arrays B and C into array A
MergeSort • Algorithm Summary – Part1 void mergesort(index low, index high){ index mid; if (low < high) { mid = (low + high) / 2 ; mergesort(low, mid); mergesort(mid+1, high); merge(low, mid, high); } } ... // in main function type[] S = new type[0..n-1]; ... mergesort(0, n-1); ...
MergeSort • Algorithm Summary – Part 2 • Merge sorted arrays B and C into array A as follows: • Repeat the following until no elements remain in one of the arrays B and C: • compare the first elements in the remaining unprocessed portions of the two arrays B and C • copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array • Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A.
MergeSort • Algorithm Summary – Part 2 keytype[] U = new keytype[0..n-1]; // temporary array … void merge(index low, index mid, index high){ index i, j, k; i = low; j = mid + 1; k = low; while (i <= mid && j <= high) { if (S[i] < S[j]) { U[k] = S[i]; i++; } else { U[k] = S[j]; j++; } k++; } if(i > mid)copy S[j] through S[high] to U[k] through U[high]; else copy S[i] through S[mid] to U[k] through U[high]; copy U[low] through U[high] to S[low] through S[high]; } i=0 j=5 i=0 j=4 j=6 j=5 i=2 i=2 j=6 i=4
MergeSort • Complexity of MergeSort • The number of unit operation: W(n) • Asymptotic Complexity • O(nlogn)by using Master Theorem! W(n) = 2W(n/2) + O(n), n>1 W(1) = 0
Master Theorem T(n) = aT(n/b) + f (n)where f(n)(nd), d 0 Master Theorem: If a < bd, T(n) O(nd) If a = bd, T(n) O(ndlog n) If a > bd, T(n) O(nlogb a ) Examples: T(n) = 4T(n/2) + n T(n) ? T(n) = 4T(n/2) + n2 T(n) ? T(n) = 4T(n/2) + n3 T(n) ?
p A[i]<p A[i]p Quick Sort • Algorithm Summary • Select a pivot (partitioning element) in the array • usually, the first element in the array • Rearrange the list so that… • all the elements in the first subarrayare smaller than the pivot • all the elements in the second subarray are larger than or equal to the pivot • Exchange the pivot with the last element in the first subarray • the pivot is now in its final position in the first subarray • Sort the two subarrays recursively
Quick Sort • Example: 15, 22, 13, 27, 12, 10, 20, 25
Quick Sort • Algorithm Summary void quicksort (index low, index high){ index pivotpoint; if (high > low) { pivotpoint = partition(low, high); quicksort(low, pivotpoint-1); quicksort(pivotpoint+1, high); } } ... // in main function type[] S = new type[0..n-1]; ... quicksort(0, n-1); ...
Quick Sort • Algorithm Summary index partition (index low, index high){ index i, j, pivotpoint; keytypepivotitem; pivotitem = S[low]; //j’s role here: it points the index of elements less than the pivotitem j = low; for(i = low + 1; i <= high; i++) if (S[i] < pivotitem) { j++; exchange S[i] and S[j]; } //j’s role here: it points the index of the location for pivotitem //after the for-loop pivotpoint = j; exchange S[low] and S[pivotpoint]; return pivotpoint; }
Quick Sort • Algorithm Summary low=1, high=8 pivotitem = s[low] pivotpoint
Quick Sort • Algorithm Summary high i j low to be investigated < pivotitem >= pivotitem ifS[i] >= pivotitem, index “i” increases high i j low to be investigated < pivotitem >= pivotitem ifS[i] < pivotitem, 1) index “j” increase, 2) swap two values in the indexs “i” and “j”, 3) index “i” increases high j low i > pivotitem < pivotitem to be investigated
Quick Sort • Complexity of Quick Sort • Worst case: sorted array! — O(n2) • Average case: random arrays — O(n log n) • Proof: skip • W(n) = W(n - 1) + n - 1, if n > 0 W(0) = 0 O(n2)
[Programming Practice 3] • Sort: MergeSortvs. Quick Sort • Visit • http://link.koreatech.ac.kr/courses/2013_1/AP-KOICA/AP-KOICA20131.html • Download “SortMain.java” and run it • Analyze the source codes • Complete the source codes while insert right codes within the two functions • public static void mergeSort(int low, int high) • public static void merge(int low, int mid, int high) • public static void quickSort(int low, int high) • public static int partition(int low, int high) • Compare the execution times of the two sort algorithms