Sorting algorithms
quick sort best case
nlg(n)
binary search best case
o(1)
binary search space
o(1)
insertion sort space
o(1)
selection sort space
o(1)
binary search worst case/avg
o(lgn)
merge sort space
o(n)
insertion sort best case
n
merge sort advantage
n * lg n in all cases, even worst case
insertion sort worst case
n^2
quick sort worst case
n^2
selection sort average case
n^2
selection sort best case
n^2
selection sort worst case
n^2
insertion sort average case
n^2 but half the time as worst case
Merge sort best case
nlg(n)
merge sort average case
nlg(n)
merge sort worst case
nlg(n)
quick sort average case
nlg(n)
selection sort advantage
fewest number of swaps...in situations (sorting integers is not one of them) where swapping our data can take a very long time due to very large amounts of it to move back and forth, swapping might have such a LARGE constant on it that it would overshadow everything else. In that case, we might want selection sort.
quick sort advantage
on average, fastest known comparison-based sorting algorithm; i.e. the only faster algorithms we know of are designed for specific kinds of data, rather than working on anything we can compare. Both quicksort and mergesort have the same order of growth, but in terms of constant factors of that n * lg n term, quicksort's constants are lower.
insertion sort disadvantage
quadratic for any randomly arranged array, i.e. it's not better than quadratic unless the array *is* sorted a lot already
selection sort disadvantage
quadratic in all cases, can't even be improved upon if the array is partially sorted. Works okay for small arrays but insertion sort works better for those arrays...so in general, Selection Sort is not generally useful. It's only useful in the one case listed as an advantage above.
quick sort disadvantage
the quadratic worst case. It's very unlikely to occur but it *can* happen, making quicksort not the best choice if you *need* to be at n * lg n time.
quick sort space
unstable partition takes o(1) at most o(logn) after partition
merge sort disadvantage
uses linear extra memory (the temporary array needed by merge)...this is especially a problem if you have a VERY large array to sort, since you might not even *have* enough memory left over to create another array that size. Also, not quite as fast as quicksort's average case, when you consider the constant factors (since they both have n * lg n order of growth)
insertion sort advantage
works well for partially sorted or completely sorted arrays; also good for small arrays since quicksort and mergesort tend to be overkill for such arrays