1.32k likes | 1.56k Views
Sorting. Joe Meehean. Sorting. Problem Arrange comparable items in list into sorted order Most sorting algorithms involve comparing item values We assume items define < operator > operator == operator. Sorting in STL. void sort( Iterator begin, Iterator end)
E N D
Sorting Joe Meehean
Sorting • Problem • Arrange comparable items in list into sorted order • Most sorting algorithms involve comparing item values • We assume items define • < operator • > operator • == operator
Sorting in STL • void sort( Iterator begin, Iterator end) • data items must override < operator • void sort( Iterator begin, Iterator end, Comparator cmp) • Comparator is comparison functor • cmp(a,b) returns true if a should go before b in the sorted list • Often implemented using quick sort
Sorting • Aspects we care about • run time • memory cost • Types of algorithms • comparison vs. non comparison • Examples of important sorting algorithms • Google Search: real world example
Run-time complexity • Obvious algorithms are O(N2) • Clever ones are O(NlogN) • special purpose sorts can go even faster • Additional considerations • does algorithm always take worst-case time? • what is the average case? • what happens when the list is already sorted?
Memory Cost • In-place • sorts list with constant extra memory • e.g., temporary variables • Not in-place • requires additional memory in relation to input size • e.g., another parallel list
2 Kinds of Sorting Algorithms • Comparison sort • compare items in the list • place smaller items near the front • fastest worst case: O(NlogN) • Non-comparison sort • sort using special properties of items • use/extrapolate additional information • e.g., non-comparison sort O(Range+N)
Heap Sort • Sorting algorithm based on heaps • Idea • insert items from unsorted list into heap • use heap::removeMin to get items out of heap in sorted order • put items back into list in sorted order
Heap Sort • Problems with this approach • Complexity not ideal • inserting N items into heap is O(NlogN) • removing N items from heap is O(NlogN) • it would be better if would could do the whole thing in O(NlogN) • Memory cost • not in-place • need original list + a heap
Heap Sort: Improved Complexity • Can “heapify” a vector/array in O(N) • convert unsorted vector into a min heap • For each parent node (N/2 to 0) • make sure its larger than its children • if its not, swap parent with largest child • shiftDown(intpos, K val) • Minor complication • vector starts at 0 • not 1 like a normal heap
Heapify P L 1 7 0 9 4 3 6 5 8 2 6 0 8 9 7 3 1 2 4 5
Heapify P L 1 7 0 9 4 3 6 5 8 2 6 0 8 9 7 3 1 2 4 5 swap
Heapify P L 1 7 2 9 4 3 6 5 8 0 6 0 8 9 7 3 1 2 4 5
Heapify P L R 1 7 2 9 4 3 6 5 8 0 6 0 8 9 7 3 1 2 4 5
Heapify P L R 1 7 2 9 4 3 6 5 8 0 6 0 8 9 7 3 1 2 4 5 swap
Heapify P L R 1 7 2 3 4 9 6 5 8 0 6 0 8 9 7 3 1 2 4 5
Heapify P L R 1 7 2 3 4 9 6 5 8 0 6 0 8 9 7 3 1 2 4 5
Heapify P L R 1 7 2 3 4 9 6 5 8 0 6 0 8 9 7 3 1 2 4 5 swap
Heapify P L R 1 8 2 3 4 9 6 5 7 0 6 0 8 9 7 3 1 2 4 5
Heapify P L R 1 8 2 3 4 9 6 5 7 0 6 0 8 9 7 3 1 2 4 5
Heapify P L R 1 8 2 3 4 9 6 5 7 0 6 0 8 9 7 3 1 2 4 5 swap
Heapify P L R 1 8 2 3 9 4 6 5 7 0 6 0 8 9 7 3 1 2 4 5 swap R P L
Heapify P L R 1 8 2 3 9 5 6 4 7 0 6 0 8 9 7 3 1 2 4 5 R P L
Heapify P L R 1 8 2 3 9 5 6 4 7 0 6 0 8 9 7 3 1 2 4 5
Heapify P L R 1 8 2 3 9 5 6 4 7 0 6 0 8 9 7 3 1 2 4 5 swap
Heapify P L R 9 8 2 3 1 5 6 4 7 0 6 0 8 9 7 3 1 2 4 5 R P L
Heapify P L R And So on… 9 8 2 3 1 5 6 4 7 0 6 0 8 9 7 3 1 2 4 5 R P L
Heapify 9 8 2 3 5 4 6 1 7 0 6 0 8 9 7 3 1 2 4 5
Heapify Complexity • O(N) • proof is somewhat complex • see Weiss 6.4.3 if interested • Intuitively • it is faster because we only need to shiftdown ½ the nodes • plus starting at bottom reduces number of shift downs • inserting each node into a heap shifts down for each insert (all the nodes)
Heap Sort: In-place • Removing an item from the heap creates a space at the end • This space is where the largest item should go in the finished array • Why don’t we just put it there • recall in heap::removeMax we return h[first] and replace h[first] with h[last] • instead lets swap h[first] with h[last]
Heap Sort: In-place swap 9 8 2 3 5 4 6 1 7 0 6 0 8 9 7 3 1 2 4 5 = Sorted Vector = Heap
Heap Sort: In-place 0 8 2 3 5 4 6 1 7 9 6 0 8 9 7 3 1 2 4 5 = Sorted Vector = Heap
Heap Sort: In-place P L R 0 8 2 3 5 4 6 1 7 9 6 0 8 9 7 3 1 2 4 5 shift down = Sorted Vector = Heap
Heap Sort: In-place P L R 8 0 2 3 5 4 6 1 7 9 6 0 8 9 7 3 1 2 4 5 shift down = Sorted Vector = Heap
Heap Sort: In-place P 8 7 2 3 5 4 6 1 0 9 6 0 8 9 7 3 1 2 4 5 shift down = Sorted Vector = Heap
Heap Sort: In-place swap 8 7 2 3 5 4 6 1 0 9 6 0 8 9 7 3 1 2 4 5 = Sorted Vector = Heap
Heap Sort: In-place P L R 3 7 2 8 5 4 6 1 0 9 6 0 8 9 7 3 1 2 4 5 shift down = Sorted Vector = Heap
Heap Sort: In-place P L R And So on… 3 7 2 8 5 4 6 1 0 9 6 0 8 9 7 3 1 2 4 5 shift down = Sorted Vector = Heap
Heap Sort: In-place 0 2 4 8 1 3 5 7 6 9 6 0 8 9 7 3 1 2 4 5 = Sorted Vector = Heap
Heap Sort Item value Position in the Array
Heap Sort Complexity • Heapify • O(N) • In-place conversion of heap into sorted array • O(NlogN) • O(N) + O(NlogN) = O(NlogN) • Costs the same if array was sorted to begin with
Quick Sort B A 0 2 4 8 1 3 5 7 6 9 0 3 1 2 4 0 3 1 2 4 A 0 2 4 8 1 3 5 7 6 9 0 6 8 9 3 7 1 2 4 5 • Fundamental Idea • if all values in sorted array A are less than all values in sorted array B • we can easily combine them • an array of size 1 is sorted
Quick Sort Algorithm • if number of items in A is one or zero, return • Choose a value from A to be the pivot • Partition A into sub-lists • all values ≤ pivot into left part • all values ≥ pivot into the right part • Return quicksort(L-part) + pivot + quicksort(R-part)
Quick Sort: On the way down 2 6 3 5 4 0 7 1
Quick Sort: On the way down 2 6 3 5 4 0 7 1 4
Quick Sort: On the way down 2 6 3 5 4 0 7 1 2 3 0 1 4 5 6 7
Quick Sort: On the way down 2 6 3 5 4 0 7 1 2 3 0 1 4 5 6 7 1
Quick Sort: On the way down 2 6 3 5 4 0 7 1 2 3 0 1 4 5 6 7 0 1 2 3
Quick Sort: On the way down 2 6 3 5 4 0 7 1 2 3 0 1 4 5 6 7 0 1 2 3 3