TheAlgorithms-PHP/Sorting/SelectionSort.php
Anamarija Papić 0bfc5b2b75 rename function selectionSorting to selectionSort.
- follows the function naming practices in the folder
- fixes "undefined function selectionSort" in SortingTest.php
- selection sort tests are now passing
2023-10-06 19:28:27 +02:00

31 lines
537 B
PHP

<?php
/**
* Selection Sort
*
* @param array $array
* @return array
*/
function selectionSort(array $array)
{
$length = count($array);
for ($i = 0; $i < $length; $i++) {
$lowest = $i;
for ($j = $i + 1; $j < $length; $j++) {
if ($array[$j] < $array[$lowest]) {
$lowest = $j;
}
}
if ($i !== $lowest) {
$temp = $array[$i];
$array[$i] = $array[$lowest];
$array[$lowest] = $temp;
}
}
return $array;
}