Internal Sort:
Bubble O(n2)
Selection O(n2)
Insertion O(n2)
Shell O(nlogn)
Merge O(nlogn)
Heap O(nlogn)
Quick Sort O(nlogn)
Tree Sort(BST) O(nlogn)
Linear Sorting:
Counting Sort O(n)
BUcket Sort O(n)
Radix Sort O(n)
External Sorting:
K chunks of data need to be sorted and a K-way merge has to be completed. Assembly Line
Problems:
pro22: Nearly Sorted. Given a array of n elements, each which is at most K positions from its target position, devise an algorithm that sort in O(nlogK).
Insert the first K elements into a binary heap of size K. Insert the next element from the array into the heap, and delete the minimum element from the heap.\
pro35: Nuts and Bolts. We are given a box which contains bolts and nuts. Assume that there are n nuts and n bolts and that each nut matches exactly one bolt. By trying to match a bolt and a nuts we can see which one is bigger, but we cannot compare two bolts or two nuts directly.
We can use divide-and-conquer to solve. Pick a random bolt B[i], Using this bolt to rearrange the array of nuts into three groups of elements:
- Nuts smaller than B[i]
- Nuts matches B[i]
- Nuts larger than B[i]