Download Merge Algorithm Analysis-Analysis of Algorithms-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!
Merge Algorithm Analysis
- Suppose total number of elements to be merged is n
- Running time is O (n)
Merge Sort Analysis
- Suppose n is a power of two, so that we always split into even halves.
- For n = 1 the time to merge sort is1 otherwise
- The time to merge sort n numbers is equal to the time to do two recursive merge sorts of size n/2, plus the time to merge, which is linear. T(1) = 1 T(n) = 2T(n/2) + n
- Solve this recurrence to find out running time.
Solution by Telescoping Sum
T(n) / n + T(n/2) / n/2 + T(n/4) / n/4 + ….+ T(2) / 2 = T(n/2) / 2 + 1 + T(n/4) / n/4 + 1 + T(n/8) / n/8 + 1 +…..+ T(1) / 1 + 1
- All the terms other than T(n) / n on right hand side cancel the terms on left hand side.
- Since n is a power of 2, these equations add up to lgn (Tree has lgn levels). T(n) / n = T(1) / 1 + lgn T(n) = n + nlgn T(n) = O(nlgn)
Solution by Substitution Method
T(n) = 2 T(n/2) + n
- Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n) = 4T (n/4) + 2n
- Again by substituting n/4, we can see 4T(n/4) = 4(2T(n/8)) + (n/4) = 8T(n/8) + n And T(n) = 8T(n/8) + 3n
- Continuing in this manner, we obtain T(n) = 2k^ T(n/2k) + k.n Using k = lgn T(n) = nT(1) + nlgn = nlgn + n T(n) = O(nlgn)
7
Complexity of Quick Sort
Average-case O(NlogN)
Worst Case: O(N^2 )
This happens when the pivot is the
smallest (or the largest) element.
Then one of the partitions is empty,
and we repeat recursively the
procedure for N-1 elements.
8
Complexity of Quick Sort
Best-case O(NlogN)
The pivot is the median of the array,
the left and the right parts have same
size.
There are logN partitions, and to
obtain each partitions we do N
comparisons (and not more than N/
swaps). Hence the complexity is
O(NlogN)
10
Worst-Case Analysis
The pivot is the smallest (or the largest) element
T(N) = T(N-1) + cN, N > 1
Telescoping:
T(N-1) = T(N-2) + c(N-1)
T(N-2) = T(N-3) + c(N-2)
T(N-3) = T(N-4) + c(N-3) …………... T(2) = T(1) + c.
11
Worst-Case Analysis
T(N) + T(N-1) + T(N-2) + … + T(2) =
= T(N-1) + T(N-2) + … + T(2) + T(1) +
c(N) + c(N-1) + c(N-2) + … + c.
T(N) = T(1) +
c times (the sum of 2 thru N)
= T(1) + c (N (N+1) / 2 -1) = O(N^2 )
13
Best-Case Analysis
Telescoping:
T(N) / N = T(N/2) / (N/2) + c
T(N/2) / (N/2) = T(N/4) / (N/4) + c
T(N/4) / (N/4) = T(N/8) / (N/8) + c
……
T(2) / 2 = T(1) / (1) + c
14
Best-Case Analysis
Add all equations:
T(N) / N + T(N/2) / (N/2) + T(N/4) / (N/4)
= (N/2) / (N/2) + T(N/4) / (N/4) + … + T(1) / (1) + c.logN
After crossing the equal terms:
T(N)/N = T(1) + cLogN T(N) = N + Nc*LogN = O(NlogN)
16
Summary
- Divide and Conquer
- Merge-Sort
- Most of the work done in Merging
- (n log(n)) time
- (n) space
- Quick-Sort
- Most of the work done in partitioning
- Average case takes (n log(n)) time
- Worst case takes (n^2 ) time
- (1) space
17
Choosing the Pivot
Some fixed element: e.g. the first, the last, the one in the middle.
Bad choice - may turn to be the smallest or the largest element, then one of the partitions will be empty
Randomly chosen (by random generator)
19
Choosing the Pivot
The median-of-three choice:
take the first, the last and the middle element.
Choose the median of these three elements.