Selection sort is a simple sorting algorithm which operates by repeatedly selecting the smallest element from the unsorted portion of the list and moving it to the sorted portion.
Implementation of selection sort in C:
- We partitioned the input array into sorted and unsorted regions; initially, the sorted region is empty, and the entire array is unsorted.
- Anything before
i
is sorted. Starting froms[i]
, the array is unsorted.
- Anything before
- To find the smallest item, we perform a linear sweep through the unsorted portion of the array.
- The smallest item is then swapped with the th item in the array (first element in unsorted region) before moving on to the next iteration.
- Selection sort performs iterations, where the average iteration takes steps, for a total of time.