efficient sorts. divide and conquer divide and conquer : chop a problem into smaller problems, solve...

23
Efficient Sorts

Upload: lenard-garrett

Post on 17-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

Efficient Sorts

Page 2: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

Divide and Conquer

• Divide and Conquer : chop a problem into smaller problems, solve those– Ex: binary search

Page 3: 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

Page 4: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

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

Page 5: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

MergeSort

• The code:

Page 6: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

Merging

• Merge is tricky bit– Need to do in

linear time– Merging two

sorted lists– Need extra storage

Page 7: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

Starting MergeSort

• MergeSort for an array allocates n item temp array to use for merging– One temp array used by all merges

Page 8: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

Analysis

• Number of items to sort cut in half each recursive call:

– logn recursive levels

Page 9: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

Analysis - Informal

• Calls at each recursive level combine to do O(n) worth of merge work

Page 10: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

Analysis - Informal

O

Page 11: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

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

Page 12: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

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

Page 13: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

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

Page 14: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

QuickSort Code

• The code:

Page 15: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

QuickSort Recursion

• Divide & Conquer power comes from splitting• What we want to see:

logn levels

Page 16: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

Average Case

• Suppose we pick random pivot– Some good, some bad:

Page 17: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

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

Page 18: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

QuickSort Recursion

• When things go bad:– n-1 levels– Work:

(n-1)(n-2)(n-3)…321

Page 19: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

Yikes

• Worst case: pivot is first/last element– Worst case is already sorted array!

Page 20: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

Alternatives

• Other options– Use middle as partition– Use median of low/middle/high elements– Use random as partition

Page 21: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

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)

Page 22: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

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

Page 23: Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search

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