Even with similar complexity of O(n^2) as Bubble Sort, selection sort is preferred when memory writes are too expensive. If you want to understand how it works, jump to bottom of the page to see a quick video. Find below the algorithm.
function selectionSort($arr) : array { $arrLength = count($arr); for($i = 0; $i < $arrLength; ++$i) { for($j = $i+1; $j < $arrLength; ++$j) { if($arr[$j] < $arr[$i]) { $min = $arr[$j]; $arr[$j] = $arr[$i]; $arr[$i] = $min; } } } return $arr; }
Source Code of Algorithm Series: https://github.com/EresDev/Algorithms
Understand Selection Sort: