Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Chapter 6. 2 Algorithms to know Insertion Sort Insertion Sort –O(n 2 ) but very efficient on partially sorted lists. Quicksort Quicksort –O(n.

Similar presentations


Presentation on theme: "Sorting Chapter 6. 2 Algorithms to know Insertion Sort Insertion Sort –O(n 2 ) but very efficient on partially sorted lists. Quicksort Quicksort –O(n."— Presentation transcript:

1 Sorting Chapter 6

2 2 Algorithms to know Insertion Sort Insertion Sort –O(n 2 ) but very efficient on partially sorted lists. Quicksort Quicksort –O(n 2 ) worst-case but O(n log n) average case. –Improvements make it the fastest practical sorting algorithm

3 Insertion Sort is(a[]) { n = a.last; for i = 2 to n { v = a[i]; j = i –1; while (j >= 1 && v = 1 && v < a[j]) { a[j+1] = a[j]; j--;} a[j+1] = v; }}

4 Insertion Sort 10 items Worst Case 10*9/2 = 45 comparisons n*(n-1)/2 = O(n 2 )

5 Insertion Sort 10 items Best Case 9 comparisons O(n)

6 Insertion Sort 10 items Average Case

7 Quicksort http://pages.stern.nyu.edu/~panos/java/Quicksort/index.html http://pages.stern.nyu.edu/~panos/java/Quicksort/index.html quicksort (a[], i, j) { if (i < j) { p = partition(a, i, j); quicksort(a, i, p-1); quicksort(a, p+1, j); }} partition(a[],i,j) { v = a[i]; h = i; for k = i+1 to j if (a[k] < v) { h++;swap(a[h],a[k]);} swap (a[i],a[h]); return h; }

8 Quicksort 31 items Best Case O(n log n) log n levels O(n) work for each level

9 Quicksort 31 items Worst Case O(n 2 ) n levels O(n) work for each level...

10 Quicksort 31 items Average Case O(n log n) log n levels O(n) work for each level Proof on page 250-252

11 Quicksort vs. Mergesort Mergesort -close to minimum number of comparisons Mergesort -close to minimum number of comparisons –But every comparison requires moving two values a[i]  c[?]  a[?]. –Mergesort always moves 2n log n values. Quicksort - does more comparisons, but less moves or swaps Quicksort - does more comparisons, but less moves or swaps –Also, once the pivot is placed, it is never moved, i.e., its in the correct position.

12 Important Concept General Sorting General Sorting –The only thing you are allowed to do is compare two items at a time. Special Sorting Special Sorting –It is possible to compare one item with every other item in one operation. –How so?

13 Decision Tree for Sorting a<=b abc b <= c a <= c b <= c abc bac acbcabbcacba

14 Decision Tree for Sorting a<=b abc b <= c a <= c d 12 permutations 6 permutations 3 permutations 2 permutations?

15 Lower-bound on General Sorting h >= log(n!) h >= log(n!) h is the minimum number of comparisons h is the minimum number of comparisons log(n!) =  (n log n) (well known 2.3.8) log(n!) =  (n log n) (well known 2.3.8) Thus, log(n!) =  (n log n) and Thus, log(n!) =  (n log n) and h =  (n log n) h =  (n log n)


Download ppt "Sorting Chapter 6. 2 Algorithms to know Insertion Sort Insertion Sort –O(n 2 ) but very efficient on partially sorted lists. Quicksort Quicksort –O(n."

Similar presentations


Ads by Google