Download presentation
Presentation is loading. Please wait.
1
Algorithms Dr. Youn-Hee Han April-May 2013
The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang Algorithms April-May 2013 Dr. Youn-Hee Han
2
Divide & Conquer The most-well known algorithm design strategy:
1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances recursively 3. Obtain solution to original (larger) instance by combining these solutions - “Combining these solutions” is optional
3
It general leads to a recursive algorithm!
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!
4
Divide & Conquer Examples Sorting: mergesort and quicksort
Binary tree traversals Binary search Multiplication of large integers Matrix multiplication: Strassen’s algorithm
5
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 MergeSort arrays B and C recursively Merge the sorted arrays B and C into array A
6
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);
7
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.
8
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=4 j=5 i=2 j=6 i=4
9
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
10
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(nd log n) If a > bd, T(n) O(nlog b 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) ?
11
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 subarray are 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 p A[i]<p A[i]p
12
Quick Sort Example: 15, 22, 13, 27, 12, 10, 20, 25
13
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);
14
Quick Sort Algorithm Summary index partition (index low, index high){
index i, j, pivotpoint; keytype pivotitem; 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;
15
Quick Sort Algorithm Summary pivotitem = s[low] pivotpoint
low=1, high=8 pivotitem = s[low] i j S[1] S[2] S[3] S[4] S[5] S[6] S[7] S[8] 비고 - 2 3 4 5 6 7 8 1 12 23 34 initial if-true final pivotpoint
16
if S[i] >= pivotitem, index “i” increases
Quick Sort Algorithm Summary low j i high < pivotitem >= pivotitem to be investigated if S[i] >= pivotitem, index “i” increases low j i high < pivotitem >= pivotitem to be investigated if S[i] < pivotitem, 1) index “j” increase, 2) swap two values in the indexs “i” and “j”, 3) index “i” increases low j high < pivotitem > pivotitem to be investigated i
17
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)
18
[Programming Practice 3]
Sort: MergeSort vs. Quick Sort Visit 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.