Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC324 Principle of CS III

Similar presentations


Presentation on theme: "ITEC324 Principle of CS III"— Presentation transcript:

1 ITEC324 Principle of CS III
Chapter 7 (Lafore’s Book) Advanced Sorting Hwajung Lee ITEC324 Principle of CS III

2 Sorting Definition: Arrange items in a predetermined order.
SummarySort.doc

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

4 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

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

6 Shellsort (7) Diminishing Gaps
You can try your own interval sequence.

7 Shellsort (8) Source Code of ShellShort

8 Shellsort (9) Computational Performance No theoretical analysis.
Based on experiment, O(N3/2) ~ O(N7/6)

9 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

10 Shellsort (11) Disadvantages
Not quite as fast as quicksort and other O(N*logN) sorts, so not optimum for very large files.

11 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

12 Quick Sort (2) Partitioning pivot value
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.)

13 Quick Sort (3) Partitioning Algorithm

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

15 Quick sort (5) (Problem) If the data is already sorted (or inversely sorted)  Degenerates to O(N2) Performance (Solution) Median-of-Three Partitioning

16 Quick sort (6) Before sorting Left center right  median is 44
86 29  median is 44 After sorting Left center right 29 44 86

17 Quick sort (7) In order to use the mechanism of the existing
recQuickSort(), put the median at the rightmost cell. Left center right 29 86 44  Source Code of Quicksort version 2

18 Quick sort (8)  Revised Source Code of Quicksort version 2
[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

19 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


Download ppt "ITEC324 Principle of CS III"

Similar presentations


Ads by Google