quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2
sortingquick sort Demo with cards? 1.Divide S into 3 parts as follows Select an element e Create L with all element is S less than e Create E with all element in S equal to e Create G with all element in S greater than e 2.Recur by quick sorting L and quick sorting G 3.Conquer by appending L to E to G Divide & Conquer (with quick sort)
sortingquick sort Demo with cards?
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort
sortingquick sort Stopping condition
sortingquick sort Less than, Equal to, Greater than
sortingquick sort pivot
sortingquick sort Get the data sets L, E, and G
sortingquick sort Recurse & conquer
sortingquick sort
sortingquick sort.. and this happens if S is sorted!
sortingquick sort
sortingquick sort Two enhancements improve selection of pivot randomise “in place” sorting uses no more space and is fast
sorting A lower bound on comparison based sorting i.e. how good can it get?
sorting A lower bound on comparison based sorting Assume we are given a sequence S, of length n, to be sorted (no duplicates) an algorithm compares pairs of elements with two outcomes x < y TRUE x < y FALSE we can represent a comparison based sorting algorithm as a decision tree i.e. represent its behaviour internal node represents a decision left is TRUE, right is FALSE there are n! possible orderings of S there must be n! leaves in our decision tree one for each possible ordering of S running time of algorithm must be at least the height of the decision tree the height of the decision tree is at least log(n!) base 2 log(n!) is O(n.log(n))
sorting stable sorting
sortingstable sorting A sorting algorithm is stable if for any two entries x and y of S such that x.getKey() == y.getKey() and x precedes y in S before sorting x precedes y after S is sorted
sorting bucket sort aka pigeonhole sort
sorting bucket sort aka pigeonhole sort PIGEONHOLE SORT
sortingbucket sort (aka pigeonhole sort) Use the sey of an object as an index into a bucket array Assume keys are in range [0,N-1] Assume we have n object to sort insert object e into bucket[e.getKey()] Complexity is O(n+N)
sortingbucket sort (aka pigeonhole sort) Use the sey of an object as an index into a bucket array Assume keys are in range [0,N-1] Assume we have n object to sort insert object e into bucket[e.getKey()] Complexity is O(n+N) How might we do this in java? What data structures would we use? Is it stable? What kind of data sets would be suitable?
sorting radix sort We will describe by example
sortingradix sort Sort on least significant digit
sortingradix sort Sort on least significant digit
sortingradix sort Sort on least significant digit
sortingradix sort Sort on least significant digit
sortingradix sort Sort on least significant digit
sortingradix sort Sort on least significant digit
sortingradix sort Sort on least significant digit
sortingradix sort Sort on least significant digit
sortingradix sort , Sort on least significant digit
sortingradix sort , Sort on least significant digit
sortingradix sort , Sort on least significant digit
sortingradix sort , Sort on least significant digit
sortingradix sort , , Sort on least significant digit
sortingradix sort , , Sort on least significant digit
sortingradix sort , 69, , Sort on least significant digit
sortingradix sort , 69, , Sort on least significant digit
sortingradix sort , 69, 29 28, 18 81, Sort on least significant digit
sortingradix sort , 69, 29 28, 18 81, Sort on least significant digit
sortingradix sort , 69, 29,39 28, 18 81, Sort on least significant digit
sortingradix sort , 69, 29,39 28, 18 81, Sort on least significant digit
sortingradix sort , 69, 29,39 28, 18 81, Sort on least significant digit
sortingradix sort , 69, 29,39 28, 18 81, Sorted on least significant digit!
sortingradix sort Sort on next (second) significant digit
sortingradix sort Sort on next (second) significant digit
sortingradix sort Sort on next (second) significant digit 81
sortingradix sort Sort on next (second) significant digit 81
sortingradix sort Sort on next (second) significant digit 81 31
sortingradix sort Sort on next (second) significant digit 81 31
sortingradix sort Sort on next (second) significant digit
sortingradix sort Sort on next (second) significant digit
sortingradix sort Sort on next (second) significant digit , 17
sortingradix sort Sort on next (second) significant digit , 17 28
sortingradix sort Sort on next (second) significant digit , 17 28
sortingradix sort Sort on next (second) significant digit , 17, 18 28
sortingradix sort Sort on next (second) significant digit , 17, 18 28
sortingradix sort Sort on next (second) significant digit 81, , 17, 18 28
sortingradix sort Sort on next (second) significant digit 81, , 17, 18 28
sortingradix sort Sort on next (second) significant digit 81, , 17,
sortingradix sort Sort on next (second) significant digit 81, , 17,
sortingradix sort Sort on next (second) significant digit 81, , 17, 18 28, 29 69
sortingradix sort Sort on next (second) significant digit 81, , 17, 18 28, 29 69
sortingradix sort Sort on next (second) significant digit 81, 89 31, 39 14, 17, 18 28, 29 69
sortingradix sort Sorted on next (second) significant digit! 81, 89 31, 39 14, 17, 18 28, 29 69
sortingradix sort We can extend this to numbers of any length We can do this on strings Considering numbers to base b require b buckets require d = log(m) scans (d is number of digits) log is to base b m is largest number to be sorted each scan is of order n therefore O(d.n)
sorting comparison of algorithms
sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort
sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort
sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort
sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort
sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort
sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort
sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort
sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort
sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort
sortingcomparison of algorithms Bubble sort Insertion sort Selection sort Merge sort Quick sort Heap sort Bucket sort Radix sort
sorting Yes, but how did you sort out your CD’s?
not coveredsorting Sift sort Shell sort Bead sort
not coveredsorting Sift sort Shell sort Bead sort
not coveredsorting Sift sort Shell sort Bead sort
not coveredsorting