30.18. Sorting Part 2¶
30.18.1. Sorting Part 2¶
30.18.1.1. Shellsort¶
30.18.1.2. Shellsort (2)¶
30.18.1.3. Mergesort¶
30.18.1.4. Mergesort cost¶
- Mergesort cost:
- Mergsort is also good for sorting linked lists.
- Mergesort requires twice the space.
30.18.1.5. Quicksort¶
30.18.1.6. Quicksort Partition¶
30.18.1.8. Quicksort Summary¶
30.18.1.10. .¶
.
30.18.1.12. .¶
.
30.18.1.14. Optimizations for Quicksort¶
- Better Pivot
- Inline instead of function calls
- Eliminate recursion
- Better algorithm for small sublists: Insertion sort
- Best: Don't sort small lists at all, do a final Insertion Sort to clean up.
30.18.1.15. Heapsort¶
30.18.1.18. Radix Sort: Linked List¶
30.18.1.19. .¶
.
30.18.1.20. Radix Sort: Array¶
30.18.1.21. Radix Sort Implementation¶
30.18.1.22. .¶
.
30.18.1.24. Empirical Analysis¶
Sort101001K10K100K1MUpDownInsertion.00023.0070.6664.987381.06744200.04129.05Bubble.00035.0202.25277.9427691.0282068070.64108.69Selection.00039.0120.6972.477356.078000069.7669.58Shell.00034.0080.141.9930.25540.440.79Shell/O.00034.0080.121.9129.05300.360.64Merge.00050.0100.121.6119.32190.830.79Merge/O.00024.0070.101.3117.21970.470.66Quick.00048.0080.111.3715.71620.370.40Quick/O.00031.0060.091.1413.61430.320.36Heap.00050.0110.162.0826.73911.571.56Heap/O.00033.0070.111.6120.83341.011.04Radix/4.00838.0810.797.9979.98087.977.97Radix/8.00799.0440.403.9940.04044.003.99
30.18.1.25. Sorting Lower Bound (1)¶
- We would like to know a lower bound for the problem of sorting
- Sorting is O(nlogn) (average, worst cases) because we know of algorithms with this upper bound.
- Sorting I/O takes Ω(n) time. You have to look at all records to tell if the list is sorted.
- We will now prove Ω(nlogn) lower bound for sorting.