SELECTION SORT

Selection sort is a basic algorithm for sorting. This sorting algorithm is a comparison-based in-place algorithm in which the list is split into two parts, the left-end sorted part and the right-end unsorted part. The sorted part is initially empty and the unsorted part is the entire list.

From the unsorted array, the smallest element is picked and switched with the leftmost element and that element becomes part of the sorted array. This method proceeds to transfer one part to the right of the unsorted array boundary.

For large data sets, this algorithm is not suitable, as its average and worst-case complexity is Ο(n2), where n is the number of products.

Working

Consider the following depicted array as an example

The entire list is searched sequentially for the first place in the sorted list. We scan the entire list and discover that 10 is the lowest value in the first position where 14 is currently stored.

And so we’re replacing 14 with 10. After one iteration of 10, which is the minimum value of the list, the first position of the sorted list is shown.

We begin to search the rest of the list in a linear way for the second location, where 33 is located.

We notice that the second lowest value in the list is 14, and that it should appear in the second position. We’re exchanging these values.

The same process is applied to the rest of the items in the array.
Following is a pictorial depiction of the entire sorting process −

Algorithm

Pseudocode