Selection Sort Working Mechanism
For Complete YouTube Video: Click Here
In this class, we will try to understand Selection Sort Working Mechanism.
C Programming is the pre-requisite couse for the Data Structures course.
Selection Sort Working Mechanism
The selection sort works with the logic of selecting the minimum element and placing it in the position.
In sorting the elements, the selection sort will divide the array into two 1. sorted part 2. unsorted part.
For better understanding, we will consider the array of elements shown below.
From the above array, we will start from the 0th index and assume the element at that index as the minimum element.
Now we will compare the minimum element will all the remaining elements.
If any element less than this is identified, that element is considered the minimum element.
After comparing all the remaining elements, the minumum element is swapped with the element in the 0th position.
From the above array, the 0th element is 5.
After comparing each element with five, the minimum element in the remaining elements is 1.
As shown below, element 1 in the 3rd index is swapped with the 5 in the 0th position.
Similarly, we will consider the element in the 1st index nine as the minimum element and compare this element with all the remaining elements to find the minimum element among the remaining elements.
The minimum element identified is four.
Nine and four will get swapped, as shown below.
The first two elements is the sorted part of the array and the remaining part of the array is the unsorted part.
Similarly, we will compare all the elements in the remaining indexes.
All the elements will be sorted by the end of all the iterations.