160 likes | 234 Views
COP 3503 - Computer Science II (Fall 2005) Week Five. By Yunjun Zhang. Analysis of Sorting Algorithm. Merge Sort Uses divide-and-conquer to obtain and O(NlogN) running time. The Merge routine take linear time to run for two sorted arrays. Analysis of Sorting Algorithm. Quick sort
E N D
COP 3503 - Computer Science II (Fall 2005)Week Five By Yunjun Zhang
Analysis of Sorting Algorithm • Merge Sort • Uses divide-and-conquer to obtain and O(NlogN) running time. • The Merge routine take linear time to run for two sorted arrays
Analysis of Sorting Algorithm • Quick sort Four Steps: • If the number of elements in S is 0 or 1, then return. • Pick any element v in S. v is called the pivot. • Partition S-{v} into two disjoint groups: L={x|x<=v} and R={x|x>v} • Return the result of Quicksort(L) followed by v followed by Quicksort(R).
Bucket sort (bin sort) • Taking advantage of existing knowledge. • Simple one: N integer number ranging from 1 to n. No comparison needed. Only O(1) time need to be used to get the result 1,2,3,…,n • For n numbers in the range of 1 to k, the running time is O(n)+O(k), O(n+k). • When k is small compared with n, running time is O(n) • What if k>>n ???
Analysis of Sorting Algorithm • Radix sort • A set of Bucket sort started from the least significant digit. • Running time O(n*logk) • To improve the speed, use larger base for the logarithm. • Why not sort from Most significant digit? • For MSD,The Number list is split into sub-problems. • All the bucket sort in Radix sort work on the same list instead.
Analysis of Sorting Algorithm • Covered Algorithms • Merge sort • Best case O(NlogN) • Worst case O(NlogN) • Average case O(NlogN) • Quick sort • Best case O(NlogN) • Worst case O(N*N) • Average case O(NlogN) • Bucket sort (bin sort) • running time is a fixed number based on the range k and number n • O(n+k) • Radix sort • Similar as above O(nlogk)
Analysis of Sorting Algorithm • 1. Sort the sequence 8,1,4,1,5,9,2,6,5 by using • Merge sort • Quick sort, with the median-of-three pivot selection and a cutoff of 3 • Bucket sort
Analysis of Sorting Algorithm • 1. Answer Details on board.
Analysis of Sorting Algorithm • 2. A sorting algorithm is stable if elements with equal key are left in the same order as they occur in the input. Which of the sorting algorithms are stable and which are not? Why? • Merge sort • Quick sort • Bucket sort • Radix sort
Analysis of Sorting Algorithm • 2. Answer • Merge sort : stable (code in book) as long as the comparisons for equality do not break the order • Quick sort: NOT stable, pivot will move its location • Bucket sort: stable. Otherwise radix sort can not be done • Radix sort: stable. Since Bucket sort is stable
Analysis of Sorting Algorithm • 3. When all keys are equal, what is the running time of • Merge sort • Quick sort • Bucket sort • Radix sort
Analysis of Sorting Algorithm • 3. Answer • Merge sort: O(NlogN) • Quick sort: O(NlogN), for the code in the book. • Bucket sort : O(1) • Radix sort: O(1)
Analysis of Sorting Algorithm • 4. When the input has been sorted, what is the running time of • Merge sort • Quick sort • Bucket sort
Analysis of Sorting Algorithm • 4. Answer • Merge sort: O(NlogN) • Quick sort: O(NlogN), for the code in the book. • Bucket sort : O(n+k)
Analysis of Sorting Algorithm • 5. When the input has been sorted in reverse order, what is the running time of • Merge sort • Quick sort • Bucket sort
Analysis of Sorting Algorithm • 5. Answer • Merge sort: O(NlogN) • Quick sort: O(NlogN), for the code in the book. • Bucket sort : O(n+k)