Insertion Sort

Insertion sort is a simple $$O(n^2)$$ sorting algorithm. It essentially works by "inserting" every element into the list one at a time.

Insertion sort is adaptive, meaning it takes less time on almost-sorted arrays than random ones.

Explanation
Initially, the sort only considers the first element. In this case, the sublist is already sorted by definition.

Then, the sort will start inserting elements into the already sorted sublist. To do this, it compares the element with the previous element in the sublist, starting at the element in the sublist closest to the current one, until it finds an element in the sublist that is not greater than the current one. Then, it simply inserts the element into the sublist so that it goes just before the one we found.

Insertion
The process that the sort uses to insert the elements is writes-optimal and is often misimplemeted, resulting in a totally new algorithm (see optimized gnome sort). It saves the element to be inserted into an auxiliary variable, writes every element before it up to the position one element up, and finally writes the element to be inserted in its correct spot. This takes exactly $$n+1$$ writes, where $$n$$ is the amount of spaces back the element needs to be pushed.

Pseudocode
procedure insertion_sort(array arr) do # Arrays are 0-indexed, so this starts at the second element for index = 1 up to length(arr)-1 do int pos = find position of index elem the_elem = arr[index] for i = index-1 down to pos do arr[i+1] = arr[i] end for arr[pos] = the_elem end for end

Optimized gnome sort
Optimized gnome sort is a variant of insertion sort that is arguably simpler but is less writes effective. The only difference between regular insertion sort and optimized gnome sort is the insertion process. Rather than doing what is described above, it simply swaps the element to be inserted with the previous one until it is in its place. This results in the same permutation of the array but takes more writes ($$2n$$ instead of $$n+1$$).

Binary insertion sort
Binary insertion sort is a variant of insertion sort that usually takes less comparisons than standard insertion sort. Instead of using a linear search, it uses a binary search to find where the element bust be inserted. This results in a search over $$n$$ elements taking $$\log_2 n$$ comparisons, which makes it better on most arrays but a little worse on almost-sorted arrays.