Working of Selection Sort with Example
So, we have an array with 5 elements (30, 15, 10, 20, 6), it has to be sorted in ascending order.
- Select the First Element as Minimum.

-
Compare minimum with the second element. If it comes out that second element is smaller than the minimum, then assign the second element as minimum.
-
Compare minimum with the third element. After that, if the third element comes out to be smaller, then assign minimum to the third element and move on, otherwise do nothing. Continue the process and go on until the last element.
- After each iteration, the minimum is placed in the front of the unsorted array.
- For each iteration, indexing starts from the first unsorted elemtent, and previous steps are followed until all the elements are sorted as per needed.
For First Interation:
.png)
For Second Iteration:
.png)
For Third Iteration:
.png)
For Fourth Iteration:
.png)
6
