Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve...
-
Upload
lenard-garrett -
Category
Documents
-
view
216 -
download
2
Transcript of Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve...
Efficient Sorts
Divide and Conquer
• Divide and Conquer : chop a problem into smaller problems, solve those– Ex: binary search
Demo
Mergesort(list)If size = 1, returnCut list into two halvesMersort(A)Mergesort(B)Merge A and B
http://computerscience.chemeketa.edu/cs160Reader/Algorithms/MergeSort2.html
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
MergeSort
• The code:
Merging
• Merge is tricky bit– Need to do in
linear time– Merging two
sorted lists– Need extra storage
Starting MergeSort
• MergeSort for an array allocates n item temp array to use for merging– One temp array used by all merges
Analysis
• Number of items to sort cut in half each recursive call:
– logn recursive levels
Analysis - Informal
• Calls at each recursive level combine to do O(n) worth of merge work
Analysis - Informal
O
Recurrence Relation
Mergesort(list)If size = 1, returnCut list into two halvesMersort(A)Mergesort(B)Merge A and B
1n? 1? T(n/2)T(n/2)n
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
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
QuickSort Code
• The code:
QuickSort Recursion
• Divide & Conquer power comes from splitting• What we want to see:
logn levels
Average Case
• Suppose we pick random pivot– Some good, some bad:
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
QuickSort Recursion
• When things go bad:– n-1 levels– Work:
(n-1)(n-2)(n-3)…321
Yikes
• Worst case: pivot is first/last element– Worst case is already sorted array!
Alternatives
• Other options– Use middle as partition– Use median of low/middle/high elements– Use random as partition
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(n2)
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
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