Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists 11324262152728.

Similar presentations


Presentation on theme: "Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists 11324262152728."— Presentation transcript:

1 Quicksort CSE 331 Section 2 James Daly

2 Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists 11324262152728 12131524262728 AB C

3 Review : Merge Sort There are O(log n) levels Each level requires O(n) work (across all sub- problems) Comparisons Copying Total runtime is O(n log n) Limitation: Not in place (requires O(n) extra memory)

4 Quicksort Fastest general-purpose comparison-based sorting algorithm Showcases a Divide-and-Conquer approach

5 Quicksort Approach: Select a pivot Partition into low and high sets (divide) Sort both parts (conquer)

6 Quicksort Example 1381924365315726750 13043312657927581 65

7 Quicksort( A, l, r ) if (l < r) then p ← Partition(A, l, r) Quicksort(A, l, p – 1) Quicksort(A, p + 1, r) Left and right indices

8 Partition( A, l, r ) pivot ← A[r] i ← l, j ← r - 1 while (i < j) while(A[i] < pivot) i++ while(A[j] > pivot) j—- if (i < j) Swap(A[i], A[j]) Swap(A[i], A[r]) return i

9 Quicksort( A, l, r ) if (l < r) then p ← Partition(A, l, r) Quicksort(A, l, p – 1) Quicksort(A, p + 1, r) O(n) T(|Low|) T(|High|) T(n) O(1)

10 Best Case Both halves get half of the items |Low| = |High| = n/2 T(n) = 1 + n + T(n/2) + T(n/2) = 1 + n + 2T(n/2) T(n) = Θ (n log n) (See Merge Sort analysis)

11 Worst Case All the items are on one side Either |Low| = n – 1 or |High| = n – 1 T(n) = 1 + n + T(n – 1) T(n) = O(n 2 ) n n-1 n-2 n-3 1

12 Average Case What if we are only kind of bad? Assume up to 90% of items end up on one side 100 990 0 88 81 8 72 7 64 Height?

13 Average Case Height Up to n( 9 / 10 ) k items in biggest part of level k Stop when only 1 item in part n( 9 / 10 ) k = 1 k = log n T(n) = Θ (n log n) [average] 10 / 9

14 Pivot Choice Our implementation always chooses the last element as the pivot Performs badly on already sorted data Choice of pivot is arbitrary

15 Median of Three Select three elements First, last, and middle items are common Make the median item be the pivot Guarantees that each part is non-empty Performs well on the sorted case Be careful when you get to small sizes!

16 Random Pivot Pick one of the elements at random to be the pivot Must be profoundly unlucky to hit the worst case An adversary cannot give you bad input without hacking your random number generator Note that generating random numbers is moderately expensive

17 Insertion Sort Insertion Sort is better than Quicksort on small inputs Stop recursing when a part has fewer than k elements Leaves the part unsorted! Each element is no more than k spots from the correct spot Insertion sort the whole array at once


Download ppt "Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists 11324262152728."

Similar presentations


Ads by Google