Select Page

Selection sort

Selection sort is a very simple sorting technique. In this sorting, the smallest element of the array is selected first, and that element is replaced with the first element of the array. After that, the second small element is selected and replaced with the second element of the array. This process continues until the entire array is sorted.

In this sorting technique, the array is divided into two parts. The first part is the sorted portion, which is written on the left side in the array. The second part is the unsorted portion, which is written on the right side in the array.

### Selection sort algorithm

Step 1: All unsorted elements in the array are compared, and the smallest element is selected, and that element is replaced with the first element of the array.

Step 2: The second smallest element is selected, and that element is replaced with the second element of the array.

Step 3: Thus, this process continues until the entire array is sorted.

Smallest algorithm

### Example of Selection sort

Suppose we have the following array, which we have to sort.

There are 6 elements in this array, so you will need 5 steps to sort it.

Step 1: The first position in the array has 11 stores. If you search in the whole array, you get the smallest value 1, and then you replace 11 with 1.

Step 2: The second position in the array has 8 stores. If you search in the whole array, you get the second smallest value 2, and then you replace 8 with 2.

Step 3: The third position in the array has 6 stores, but there will be no change in this place because there is no element smaller than 6, which means that it is already sorted.

Step 4: The fourth position in the array has 11 stores. If you search in the whole array, you get the second smallest value 8, and then you replace 11 with 8.

Step 5: The fifth position in the array has 9 stores, but there will be no change in this place because there is no element smaller than 9, which means that it is already sorted.

Output