mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
0bfc5b2b75
- follows the function naming practices in the folder - fixes "undefined function selectionSort" in SortingTest.php - selection sort tests are now passing
31 lines
537 B
PHP
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;
|
|
}
|