What is selection sort?

Answer

Selection sort divides the array into sorted (left) and unsorted (right) portions. In each pass, it finds the minimum element in the unsorted portion and swaps it with the first unsorted element, growing the sorted portion by one. Algorithm: for i from 0 to n-2, find index of minimum in arr[i..n-1], swap arr[i] with arr[minIndex]. Time: O(n²) all cases (always scans remaining elements even if sorted). Space: O(1) in-place. Stable: NO (swapping can change relative order of equal elements — can be made stable with insertion instead of swap). Advantage over bubble sort: fewer swaps — exactly n-1 swaps always. Useful when write operations are expensive (flash memory). Disadvantage: never terminates early — always O(n²) even on sorted input. Comparison: insertion sort is generally better than selection sort for partially sorted data (O(n) best case vs O(n²)). Both are O(n²) average/worst. Selection sort is useful as an educational example of in-place sorting and demonstrates finding minimums, but is not used in practice for general sorting.