Selection Sort Algorithm
Selection sort is conceptually the most simplest sorting algorithm. This algorithm will
first find the smallest element in the array and swap it with the element in
the first position, then it will find the second smallest element and swap it with the
element in the second position, and it will keep on doing this until the entire array is
sorted.
It is called selection sort because it repeatedly selects the next-smallest element
and swaps it into the right place.
How Selection Sort Works?
Following are the steps involved in selection sort(for sorting a given array in
ascending order):
1. Starting from the first element, we search the smallest element in the array, and
replace it with the element in the first position.
2. We then move on to the second position, and look for smallest element present in the
subarray, starting from index 1, till the last index.
3. We replace the element at the second position in the original array, or we can say at
the first position in the subarray, with the second smallest element.
4. This is repeated, until the array is completely sorted.
Let's consider an array with values {3, 6, 1, 8, 4, 5}
Below, we have a pictorial representation of how selection sort will sort the given
array.
In the first pass, the smallest element will be 1, so it will be placed at the first
position.
Then leaving the first element, next smallest element will be searched, from the
remaining elements. We will get 3 as the smallest, so it will be then placed at the
second position.
Then leaving 1 and 3(because they are at the correct position), we will search for the
next smallest element from the rest of the elements and put it at third position and
keep doing this until array is sorted.
Finding Smallest Element in a subarray
In selection sort, in the first step, we look for the smallest element in the array and
replace it with the element at the first position. This seems doable, isn't it?
Consider that you have an array with following values {3, 6, 1, 8, 4, 5}. Now
as per selection sort, we will start from the first element and look for the smallest
number in the array, which is 1 and we will find it at the index 2. Once the smallest
number is found, it is swapped with the element at the first position.
Well, in the next iteration, we will have to look for the second smallest number in the
array. How can we find the second smallest number? This one is tricky?
If you look closely, we already have the smallest number/element at the first position,
which is the right position for it and we do not have to move it anywhere now. So we
can say, that the first element is sorted, but the elements to the right, starting from
index 1 are not.
So, we will now look for the smallest element in the subarray, starting from index 1,
to the last index.
Confused? Give it time to sink in.
After we have found the second smallest element and replaced it with element on
index 1(which is the second position in the array), we will have the first two positions
of the array sorted.
Then we will work on the subarray, starting from index 2 now, and again looking for
the smallest element in this subarray.
Iterative Code : - https://www.geeksforgeeks.org/selection-sort/
Recursive Code:- https://www.techiedelight.com/selection-sort-iterative-recursive/
Complexity Analysis of Selection Sort
Worst Case Time Complexity [ Big-O ]: O(n2)
Best Case Time Complexity [Big-omega]: O(n2)
Average Time Complexity [Big-theta]: O(n2)
Space Complexity : O(1)
Insertion Sort Algorithm
Consider you have 10 cards out of a deck of cards in your hand. And they are
sorted, or arranged in the ascending order of their numbers.
If I give you another card, and ask you to insert the card in just the right position, so
that the cards in your hand are still sorted. What will you do?
Well, you will have to go through each card from the starting or the back and find the
right position for the new card, comparing it's value with each card. Once you find the
right position, you will insert the card there.
Similarly, if more new cards are provided to you, you can easily repeat the same
process and insert the new cards and keep the cards sorted too.
This is exactly how insertion sort works. It starts from the index 1(not 0), and each
index starting from index 1 is like a new card, that you have to place at the right
position in the sorted subarray on the left.
Following are some of the important characteristics of Insertion Sort:
1. It is efficient for smaller data sets, but very inefficient for larger lists.
2. Insertion Sort is adaptive, that means it reduces its total number of steps if a partially
sorted array is provided as input, making it efficient.
3. It is better than Selection Sort and Bubble Sort algorithms.
4. Its space complexity is less. Like bubble Sort, insertion sort also requires a single
additional memory space.
5. It is a stable sorting technique, as it does not change the relative order of elements
which are equal.
How Insertion Sort Works?
Following are the steps involved in insertion sort:
1. We start by making the second element of the given array, i.e. element at index 1,
the key. The key element here is the new card that we need to add to our existing
sorted set of cards(remember the example with cards above).
2. We compare the key element with the element(s) before it, in this case, element at
index 0:
o If the key element is less than the first element, we insert the key element
before the first element.
o If the key element is greater than the first element, then we insert it after the
first element.
3. Then, we make the third element of the array as key and will compare it with
elements to it's left and insert it at the right position.
4. And we go on repeating this, until the array is sorted.
Let's consider an array with values {5, 1, 6, 2, 4, 3}
Below, we have a pictorial representation of how bubble sort will sort the given array.
As you can see in the diagram above, after picking a key, we start iterating over the
elements to the left of the key.
We continue to move towards left if the elements are greater than the key element
and stop when we find the element which is less than the key element.
And, insert the key element after the element which is less than the key element.
Iterative Code : - https://www.geeksforgeeks.org/insertion-sort/
Recursive Code:- https://www.geeksforgeeks.org/c-program-for-recursive-insertion-sort/
Complexity Analysis of Insertion Sort
Worst Case Time Complexity [ Big-O ]: O(n2)
Best Case Time Complexity [Big-omega]: O(n)
Average Time Complexity [Big-theta]: O(n2)
Space Complexity: O(1)