1 / 16

COP 3503 - Computer Science II (Fall 2005) Week Five

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

linda-hicks
Download Presentation

COP 3503 - Computer Science II (Fall 2005) Week Five

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP 3503 - Computer Science II (Fall 2005)Week Five By Yunjun Zhang

  2. 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

  3. 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).

  4. 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 ???

  5. 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.

  6. 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)

  7. 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

  8. Analysis of Sorting Algorithm • 1. Answer Details on board.

  9. 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

  10. 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

  11. Analysis of Sorting Algorithm • 3. When all keys are equal, what is the running time of • Merge sort • Quick sort • Bucket sort • Radix sort

  12. 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)

  13. Analysis of Sorting Algorithm • 4. When the input has been sorted, what is the running time of • Merge sort • Quick sort • Bucket sort

  14. 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)

  15. 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

  16. 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)

More Related