190 likes | 205 Views
Learn about Shellsort, Quick Sort, and Radix Sort algorithms for sorting a sequence of numbers. Understand their advantages, disadvantages, and time complexities. Explore their source codes and computational performance.
E N D
Chapter 7 (Lafore’s Book) Advanced Sorting Hwajung Lee ITEC324 Principle of CS III
Sorting • Definition: Arrange items in a predetermined order. • SummarySort.doc
Shellsort (1) • The Shellsort is named for Donard L. Shell, the computer scientist who discovered it in 1959. • Objective: To sort a sequence of numbers • Method: Based on the insertion sort, but adds a new feature that dramatically improves the insertion sort’s performance.
Shellsort (2) • Applet: ShellSort • Diminishing Gaps • Also called Interval sequence or gap sequence • Time complexity of Shell sort depends mainly on the interval sequence • Suggested interval sequence
Shellsort (6)Diminishing Gaps Interval sequence suggested by Knuth based on the following equation h = h*3 + 1 • Known as the best interval sequence in terms of time complexity until now.
Shellsort (7)Diminishing Gaps You can try your own interval sequence.
Shellsort (8) • Source Code of ShellShort
Shellsort (9) • Computational Performance • No theoretical analysis. • Based on experiment, O(N3/2) ~ O(N7/6)
Shellsort (10) • Advantages • Good for medium-sized arrays, perhaps up to a few thousand items. • Much faster than O(N2) sorts • Very easy to implement – The code is short and simple • Solves a problem of the insertion sort that it requires too many copies
Shellsort (11) • Disadvantages • Not quite as fast as quicksort and other O(N*logN) sorts, so not optimum for very large files.
Quick Sort (1) • Discovered by C.A.R. Hoare in 1962. • The most popular sorting algorithm due to its efficiency. • Objective: To sort a sequence of numbers • Method: Based on partitioning data. • Efficiency (time complexity): O(N*logN) • Applet: Quick Sort1, Quick Sort 2
Quick Sort (2) • Partitioning • Using partitioning, data are divided into two groups such that all the items with a key value higher than a specified amount are in one group, and all the items with a lower key value are in another. • pivot value • it is the value used to determine into which of the to groups an item is placed. ( cf. key values: data which need to be sorted.)
Quick Sort (3) • Partitioning Algorithm
Quick sort in details (4) • Three basic steps: Textbook page 334 fig 7.8 • Partition the array or subarray into left(smaller keys) and right (larger keys) groups. • call ourselves to sort the left group. • call ourselves to sort the right group.
Quick sort (5) (Problem) If the data is already sorted (or inversely sorted) Degenerates to O(N2) Performance (Solution) Median-of-Three Partitioning
Quick sort (6) Before sorting Left center right median is 44 After sorting Left center right
Quick sort (7) In order to use the mechanism of the existing recQuickSort(), put the median at the rightmost cell. Left center right Source Code of Quicksort version 2
Quick sort (8) [Handling Small Partitions] If you use the median-of-three partitioning method, the quicksort algorithm will not work for partitions of three or fewer items. Cutoff point • Cutoff point 3 • Cutoff point 9 is recommended by Knuth and considered where the best performance lies. But, it will depend on your computer, operating system, compiler (or interpreter), and so on. Revised Source Code of Quicksort version 2
Radix Sort • Radix: a base of a system of numbers • Algorithm of Radix Sort • Time Complexity of Radix Sort: O(K(N+d)), where • K = the number of digits in keys • N = the number of key values (= the number of inputs) • d = the number of radix