Selection Sort Algorithm

For Complete YouTube Video: Click Here

In this class, we will try to understand Selection Sort Algorithm.

We have already discussed the Selection Sort Working Mechanism in our previous class.

Selection Sort Algorithm

The Selection Sort Algorithm is shown below.

Selection Sort Algorithm 1
Selection Sort

We will try to understand the working of the selection sort by using the array as shown below.

Selection Sort Algorithm 2
Selection Sort

The outer for loop of the algorithm will iterate from 0 to 3.

The second line of code min = “i” states the index of the minimum element.

Assuming that the minimum element is at the ith index, the inner for loop will start its iteration.

The inner for loop will now iterate from i + 1 to n.

In every iteration, the element at the A[j] < A[min] comparision is done.

If A[j] < A[min] is true then the index of min = j.

After completing all iterations, the index of the min element is identified.

Now, the elements A[i] and A[j] get swapped.

After completion of all the iterations, the elements will be in sorted order.