Selection sort is a straightforward sorting algorithm that iteratively selects the smallest unsorted element and places it in its correct position. This technique works by maintaining a divided array of two parts: one part sorted and the other part unsorted.
While selection sort is simple to understand, it is not a stable sorting algorithm. The stability of a sorting algorithm refers to its ability to maintain the relative order of elements with the same value. This characteristic is crucial when sorting more than one time by different criteria.
- Selection sort works by swapping elements as it finds the smallest item in the unsorted part of the array and moves it to the start.
- This method of swapping can disrupt the relative order of equal elements.
- For instance, when duplicate values are encountered, the one that appears later can be brought forward, altering the original sequence preserved in the sorted portion.
Although selection sort is not stable, it is beneficial in scenarios where memory usage is more critical than speed or order preservation, as it does not require additional memory for auxiliary arrays.