Selection Sort and Insertion Sort
Selection Sort and Insertion Sort
The Concept:
Selection Sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-
based algorithm in which the list is divided into two parts, the sorted part at the left end and the
unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire
list.
The smallest element is selected from the unsorted array and swapped with the leftmost element,
and that element becomes a part of the sorted array. This process continues moving unsorted
array boundary by one element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexities are of
Ο(n2), where n is the number of items.
12 31 25 8 33 17 40 42
For the first position in the sorted list, the whole list is scanned sequentially. The first position where 12 is
stored presently, we search the whole list and find that 8 is the lowest value.
12 31 25 8 33 17 40 42
So we replace 12 with 8. After one iteration 8, which happens to be the minimum value in the list, appears
in the first position of the sorted list.
8 31 25 12 33 17 40 42
For the second position, where 31 is residing, we start scanning the rest of the list in a linear manner.
8 31 25 12 33 17 40 42
We find that 12 is the second lowest value in the list and it should appear at the second place. We swap
these values.
8 31 25 12 33 17 40 42
After two iterations, two least values are positioned at the beginning in a sorted manner.
8 12 25 31 33 17 40 42
The same process is applied to the rest of the items in the array.
Following is a pictorial depiction of the entire sorting process
8 12 25 31 33 17 40 42
8 12 25 31 33 17 40 42
8 12 19 31 33 25 40 42
8 12 19 31 33 25 40 42
8 12 19 25 33 31 40 42
8 12 19 25 33 31 40 42
8 12 19 25 33 31 40 42
8 12 19 25 31 35 40 42
8 12 19 25 31 33 40 42
Insertion Sort
We take an unsorted array for our example.
12 31 25 8 33 17 40 42
Insertion sort compares the first two elements.
12 31 25 8 33 17 40 42
It finds that both 12 and 31 are already in ascending order. For now, 12 is in sorted sub-list.
12 31 25 8 33 17 40 42
Insertion sort moves ahead and compares 31 with 25.
12 31 25 8 33 17 40 42
And finds that 31 is not in the correct position.
12 31 25 8 33 17 40 42
It swaps 31 with 25. It also checks with all the elements of sorted sub-list. Here we see that the sorted sub-
list has only one element 12, and 25 is greater than 12. Hence, the sorted sub-list remains sorted after
swapping.
12 25 31 8 33 17 40 42
By now we have 12 and 25 in the sorted sub-list. Next, it compares 31 with 8.
12 25 31 8 33 17 40 42
These values are not in a sorted order.
12 25 31 8 33 17 40 42
So we swap them.
12 25 8 31 33 17 40 42
However, swapping makes 25 and 8 unsorted.
12 25 8 31 33 17 40 42
Hence, we swap them too.
12 8 25 31 33 17 40 42
Again we find 12 and 8 in an unsorted order.
12 8 25 31 33 17 40 42
We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.
8 12 25 31 33 17 40 42
This process goes on until all the unsorted values are covered in a sorted sub-list. Now we shall see some
programming aspects of insertion sort.