Insertion sort, involves "inserting" unsorted elements, one at a time, in the appropriate position in the sorted set.
Initially, an assumption is made that every element to the left of the second element ( = element at position 1), which is of course the first element ( = element at position 0), belongs to the sorted set and that second element ( = element at position 1) is meant to be inserted into that sorted set at the correct position.
Visualizing it, would be:
Position → 0 1 2 3 4 5
Values → 8 | 2 16 5 1 4
This second element ( = element at position 1) is referred as the chosen/marked element, and is compared to every element in the sorted set.
And if the marked element is smaller than the element in sorted set it is compared against , then that element from sorted set is copied to the position to its right. So the compared sorted element at position at position 1, is copied to position 2.
If however, the marked element is bigger than the element in the sorted set that it is being compared with, then the marked element is inserted in the position of the element, that it was previously moved against (not currently compared against). So a marked element that is found to be greater than element at position at position 1, is copied to position 2.
If M stands for marked element, and P for the potential position that the marked element in the unsorted set may be inserted if it's value is greater than the value of element at position P-1 or if P is 0, and a yellow line divides the unsorted part (to the right of the yellow line) and sorted part (to the left of the yellow line), then watching the below animation can help with understanding how insertion sort works:
After M is inserted at one of the positions, then we repeat the steps with M set to the next unsorted element, to the right of the yellow line in above animation, until no elements are left in the unsorted part, or in other words no elements are on the right-side of the yellow line in above animation.
Since the unsorted element that is marked, will be the first element of the unsorted set, it will be essentially compared with the element before it, which will be the last element of the sorted set. If the marked element is found to be bigger than the last element of the sorted set then nothing need to be done, and the next element in the unsorted set is marked for comparison.
However, if the marked element is found to be smaller than the last element in the sorted set, then the last element will be copied to the position of the marked element (effectively moving it right), and the marked element is now compared against the last but first element of the sorted set, and the steps are repeated until we either encounter a element in sorted set that is smaller than marked element, or we reached the first element of the sorted set. If we reached the first element of the sorted set, then we effectively insert our marked element there since that will be the smallest of them all.
Each step involves comparing the marked element, with a element in the sorted set and moving it if needed.
The steps involved in brief are:
- Chose the next unsorted element
- Insert it into appropriate position in the sorted set using comparison.
- Repeat steps 1 through 2, until there are no more elements in the unsorted set.
To summarize, the algorithm, watch the my video below about the workings of a InsertionSort :
To convert, this InsertionSort algorithm into a program, watch the my video below about Writing a InsertionSort Program: