mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
23 lines
384 B
PHP
23 lines
384 B
PHP
<?php
|
|
|
|
/**
|
|
* Insertion Sort
|
|
*
|
|
* @param array $array
|
|
* @return array
|
|
*/
|
|
function insertionSort(array $array)
|
|
{
|
|
for ($i = 1; $i < count($array); $i++) {
|
|
$currentVal = $array[$i];
|
|
|
|
for ($j = $i - 1; $j >= 0 && $array[$j] > $currentVal; $j--) {
|
|
$array[$j + 1] = $array[$j];
|
|
}
|
|
|
|
$array[$j + 1] = $currentVal;
|
|
}
|
|
|
|
return $array;
|
|
}
|