Insertion sort is probably the best quadratic sort. We will see linearithmic sorts that are faster, but not as adaptive.
The basic idea is this: there will be a growing sorted region on the left side. We make one pass through the array, from left to right. Each time, we find where in the sorted region it should go, and then move it down there.
This is still quadratic: we do a linear number of search/moves, and each search/move is itself linear.
There are two ways we can do the search: we could do a binary search or we could do an exhaustive search starting at the right end. The binary search works better if we are sorting random data:
But the exhaustive search works better on semisorted data:
So which one you choose should depend on what kind of data you expect to sort. But either way, insertion sort is clearly adaptive.Insertion sort is also stable, as seen here:
Every sort from this point forward is going to be linearithmic. However, linearithmic sorts tend to not be adaptive, and they tend to have complicated overhead calculations. Because it is simple, insertion sort can sort small arrays quickly. Because of this, some more complicated algorithms actually switch over to insertion sort, to sort some small subarray.
Here's everything so far:
On to heap sort...