Download presentation
Presentation is loading. Please wait.
Published byLenard Garrett Modified over 9 years ago
1
Efficient Sorts
2
Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search
3
Demo Mergesort(list) If size = 1, return Cut list into two halves Mersort(A) Mergesort(B) Merge A and B http://computerscience.chemeketa.edu/cs160Reader/Algorithms/MergeSort2.html
4
Merge Sort Partition phase – Recursive calls – Break up list until down to size of 1 Merge phase – Return from recursive calls – Build list back up
5
MergeSort The code:
6
Merging Merge is tricky bit – Need to do in linear time – Merging two sorted lists – Need extra storage
7
Starting MergeSort MergeSort for an array allocates n item temp array to use for merging – One temp array used by all merges
8
Analysis Number of items to sort cut in half each recursive call: – logn recursive levels
9
Analysis - Informal Calls at each recursive level combine to do O(n) worth of merge work
10
Analysis - Informal
11
Recurrence Relation Mergesort(list) If size = 1, return Cut list into two halves Mersort(A) Mergesort(B) Merge A and B 1 n? 1? T(n/2) n
12
QuickSort Pick a pivot – value that will separate list into smaller values and larger values Partition list : move smaller values to left, larger values to right Sort each half
13
Partitioning Make first item in range 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
14
QuickSort Code The code:
15
QuickSort Recursion Divide & Conquer power comes from splitting What we want to see: logn levels
16
Average Case Suppose we pick random pivot – Some good, some bad:
17
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 Divide work remaining by 4/3 Still log work – bigger by constant factor
18
QuickSort Recursion When things go bad: – n-1 levels – Work: (n-1) (n-2) (n-3) … 3 2 1
19
Yikes Worst case: pivot is first/last element – Worst case is already sorted array!
20
Alternatives Other options – Use middle as partition – Use median of low/middle/high elements – Use random as partition
21
BigO If everything goes right… – Cut problem in half each time : logn steps?? – O(n) to do partition at each level – O(nlogn) But can be O(n 2 )
22
Quick Sort vs Merge Sort Both O(nlogn) on average – Only mergesort guarantees it But… – MergeSort generally requires O(n) extra space for temp array – QuickSort tends to work better with cache access Mergesort is stable, quicksort is not
23
Hybrid Algorithms Real world algorithms tend to mix approaches – 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.