Topic 17 Fast Sorting
Topic 17 Fast Sorting
Faster Sorting
"The bubble sort seems to have
nothing to recommend it, except
a catchy name and the fact that it
leads to some interesting
theoretical problems."
- Don Knuth
Previous Sorts
8 Insertion Sort and Selection Sort are both
average case O(N2)
8 Today we will look at two faster sorting
algorithms.
– quicksort
– mergesort
Big O of Quicksort?
CS314 Fast Sorting 7
private static void swapReferences(Object[] a, int index1, int index2) {
Object tmp = a[index1];
a[index1] = a[index2];
a[index2] = tmp;
}
private void quicksort(Comparable[] data, int start, int stop) {
if(start < stop) {
int pivotIndex = (start + stop) / 2;
// Place pivot at start position
swapReferences(data, pivotIndex, start);
Comparable pivot = data[start];
// Begin partitioning
int j = start;
// from first to j are elements less than or equal to pivot
// from j to i are elements greater than pivot
// elements beyond i have not been checked yet
for(int i = start + 1; i <= stop; i++ ) {
//is current element less than or equal to pivot
if (data[i].compareTo(pivot) <= 0) {
// if so move it to the less than or equal portion
j++;
swapReferences(data, i, j);
}
}
//restore pivot to correct spot
swapReferences(data, start, j);
quicksort( data, start, j - 1 ); // Sort small elements
quicksort( data, j + 1, stop ); // Sort large elements
} // else start >= stop, 0 or 1 element, base case, do nothing
}
Best Worst
A. O(NlogN) O(N2)
B. O(N2) O(N2)
C. O(N2) O(N!)
D. O(NlogN) O(NlogN)
E. O(N) O(NlogN)
When implementing
one temporary array
is used instead of
multiple temporary
arrays.
Why?
times in seconds
CS314 Fast Sorting 18
Comparison of Various Sorts (2011)
Num Items Selection Insertion Quicksort Merge Arrays.sort
1000 0.002 0.001 - - -
2000 0.002 0.001 - - -
4000 0.006 0.004 - - -
8000 0.022 0.018 - - -
16000 0.086 0.070 0.002 0.002 0.002
32000 0.341 0.280 0.004 0.005 0.003
64000 1.352 1.123 0.008 0.010 0.007
128000 5.394 4.499 0.017 0.022 0.015
256000 21.560 18.060 0.035 0.047 0.031
512000 86.083 72.303 0.072 0.099 0.066
1024000 ??? ??? 0.152 0.206 0.138
2048000 0.317 0.434 0.287
4096000 0.663 0.911 0.601
8192000 1.375 1.885 1.246
Comparison of Various Sorts (2020)
Num Selection Insertion Quicksort Mergesort Arrays. Arrays.so Arrays.
rt(Integer) parallelSort
Items sort(int)
1,000 <0.001 <0.001 - - - - -
2,000 0.001 <0.001 - - - - -
4,000 0.004 0.003 - - - - Speeds
8,000 0.017 0.010 - - - - up????
16,000 0.065 0.040 0.002 0.002 0.003 0.011 0.007
32,000 0.258 0.160 0.002 0.003 0.002 0.008 0.003
64,000 1.110 0.696 0.005 0.008 0.004 0.011 0.001
128,000 4.172 2.645 0.011 0.015 0.009 0.024 0.002
256,000 16.48 10.76 0.024 0.034 0.018 0.051 0.004
512,000 70.38 47.18 0.049 0.068 0.040 0.114 0.008
1,024,000 - - 0.098 0.143 0.082 0.259 0.017
2,048,000 - - 0.205 0.296 0.184 0.637 0.035
4,096,000 - - 0.450 0.659 0.383 1.452 0.079
8,192,000 - - 0.941 1.372 0.786 3.354 0.148
Concluding Thoughts
8 Language libraries often have sorting
algorithms in them
– Java Arrays and Collections classes
– C++ Standard Template Library
– Python sort and sorted functions
8 Hybrid sorts
– when size of unsorted list or portion of array is
small use insertion sort, otherwise use
O(N log N) sort like Quicksort or Mergesort
Fast Sorting 21
Concluding Thoughts
8 Sorts still being created!
8 Timsort (2002)
– created for python version 2.3
– now used in Java version 7.0+
– takes advantage of real world data
– real world data is usually partially sorted,
not totally random
8 Library Sort (2006)
– Like insertion sort,
but leaves gaps for later elements
Fast Sorting 22
CS314 Fast Sorting 23