Real-World Sorting (Not on Exam) Sorting Algorithms So Far
Total Page:16
File Type:pdf, Size:1020Kb
Real-world sorting (not on exam) Sorting algorithms so far Worst case Average case Best case Bubblesort O(n2) O(n2) O(n) or O(n2) Selection O(n2) No clear winner...O(n2) O(n2) sort the best algorithms Insertion O(n2) combineO(n2) ideas O(n) sort from several Quicksort O(n2) O(n log n) O(n log n) Mergesort O(n log n) O(n log n) O(n log n) Introsort Quicksort: fast in practice, but O(n2) worst case Introsort: ● Start with Quicksort ● If the recursion epth gets too big, switch to heapsort, which is O(n log n) !lus stan ar Quicksort optimisations: ● "hoose pivot #ia median-of$three ● Switch to insertion sort for small arra%s Use by e.g. C+' S(), .N*(, ... Dual-pivot quicksort Instea of one pivot, pick two Instea of partitioning the arra% into two pieces, partition it into three pieces ● If pivots are x an y, then: ● elements < x ● elements > x an < y ● elements > % Same complexity as Quicksort, but fewer recursive calls because the arra% gets split up .uicker Use b% /a#a for primitive types (int, ...) Traditional merge sort (a recap) 1 ! " 4 # 1 $ 5 split split 1 ! " 4 # 1 $ 5 split split split split 1 ! " 4 # 1 $ 5 1 # 1 1 ! " 4 # 1 $ 5 1 ! " 4 # 1 $ 5 1 1 # 1 ! 4 " 1 # 5 $ merge merge merge merge 1 4 ! " 1 # 5 $ merge merge 1 1 # 4 5 ! $ " %atural merge sort (ra itional merge sort splits the input list into single elements before merging ever%thing back together again 0etter i ea: split the input into runs ● 1 run is a se.uence of increasing elements ● ...or a se.uence of ecreasing elements ● 2irst re#erse all the ecreasing runs, so the% become increasing ● 3en merge all the runs together 1 ! " 4 # 1 $ 5 split split split split 1 ! " 4 # 1 $ 5 reverse 1 # 4 merge merge merge merge 1 1 # 4 ! " 5 $ merge merge 1 1 # 4 5 ! $ " %atural merge sort 0ig a #antage: O(n) time for sorte data ● ...and re#erse$sorted ata ● ...and 4almost”$sorted ata "omplexit%: O(n log r), where r is the number of runs in the input ● ...worst case, each run has two elements, so r = n/2, so O(n log n) Use by G9" 1 ! " 4 # 1 $ 5 split split split split 1 ! " 4 # 1 $ 5 re#erse (otal time is O (log r) O(n1 log# r)4: 4le#els5 merge merge merge merge 1 1 # 4 ! " 5 $ merge merge 1 1 # 4 5 ! $ " O(n) time per le#el Timsort Natural mergesort is reall% good on sorte /nearl%$sorte ata ● ;ou get long runs so not many merges to o 0ut not so goo on ran om data ● Short runs so many merges I ea of (imsort: on short ran om parts of the list, switch to insertion sort 9ow to etect ran omness< ● Se#eral short runs ne-t to one another 1 ! " 4 # 1 $ 5 split split split 1 =on>t! split" 3 7 54 # 1 $ 5 into two runs, it>s too short:re#erse 1 # 4 merge merge insertion sort 1 1 # 4 ! " 5 $ merge merge 1 1 # 4 5 ! $ " Timsort SpeciBcall%: ● If we come across a short run, Coin it together with all following short runs until we reach a threshold ● 3en use insertion sort on that part 1lso some optimisations for merge: ● Merge smaller runs together first ● If the merge begins with several elements from the same arra%, use binary search to find out how many and then copy them all in one go Use in Ja#a for arrays of objects, P%thon http:77en.wikipedia.org7wiki7(imsort Summar& No one-siEe fits all answer ● 0est o#erall comple-it%: natural mergesort ● 0ut .uicksort has smaller constant factors =iFerent algorithms are goo in diFerent situations ● ...%ou should Bnd out which in the lab :) 0est sorting functions combine i eas from several algorithms ● Introsort: .uicksort+heapsort+insertion sort ● (imsort: natural mergesort'insertion sort.