Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quick Sort.

Similar presentations


Presentation on theme: "Quick Sort."— Presentation transcript:

1 Quick Sort

2 QuickSort Pick a pivot – value that will separate list into smaller values and larger values

3 QuickSort Pick a pivot – value that will separate list into smaller values and larger values Partition list : Move values < pivot to left of pivot Move values > pivot to right of pivot Recursively sort halves

4 QuickSort Algorithm Non-recursive function to kickstart
Recursive internal

5 Partitioning Using first item as pivot:
i starts at beginning, moves up to find elements that are larger than pivot j starts at end, moves down to find elements smaller than pivot swap mismatched pairs

6 Partitioning Outer loop: continue until i and j meet
First inner – advance i until find big item or hit j Second inner – advance j until find big item or hit i Partition Array between low and high: pivotValue = value at low i = low+1 //left maker j = high //right marker while i <= j while a[i] <= pivotValue and i <= j increase i while a[j] >= pivotValue and i <= j decrease j if i < j swap values at i and j increase i decrease j swap values at low and j return new location of pivot

7 QuickSort Analysis Divide & Conquer power comes from splitting
What we want to see: logn levels

8 QuickSort Analysis What we want to see: logn levels
Each level has n work to partition O(nlogn)

9 Recurrence Relation Quicksort(array) 1 n (for partition call) T(n/2)
If size = 1, return Pivot = Partition(array) Quicksort(0, pivot) Quicksort(pivot, end) 1 n (for partition call) T(n/2)

10 Average Case But pivot will not always chop list in half:
Some good, some bad:

11 Average Case Suppose we pick random pivot from n remaining:
50% chance to be in gray Anything n gray leaves at most ¾ work for next level Divides work remaining by 4/3 Still log work – bigger by constant factor

12 QuickSort Recursion When things go REALLY bad:
Everything larger than pivot every time Work (n-1) (n-2) (n-3) … 3 2 1 O(n2)

13 Yikes Worst case: pivot is smallest/largest element
Worst case is already sorted array!

14 Alternatives Fixing bad pivot Then quicksort as normal
Use middle item in range as partition Let mid be (low + high) / 2 Start by swapping array[mid] with array[low] Use random item in range as partition Let x be a random number between low and high Start by swapping array[x] with array[low] Then quicksort as normal

15 BigO If everything goes right… But can be O(n2)
Cut problem in half each time : logn levels O(n) to do partition at each level O(nlogn) But can be O(n2) With first item as pivot If we get really unlucky with other strategy

16 Quick Sort vs Merge Sort
Both O(nlogn) on average Only mergesort guarantees it But… MergeSort generally requires O(n) extra space QuickSort has better constant factors QuickSort tends to use cache better Mergesort is stable, quicksort is not

17 Hybrid Algorithms Real world algorithms tend to mix approaches C++
Timsort : mix of merge & insertion sort Java & Python Introsort : quicksort until a certain depth, then insertion or heap sort C++ Sort = introspective quicksort Stable_sort = introspective mergesort


Download ppt "Quick Sort."

Similar presentations


Ads by Google