mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 23:28:14 +01:00
16 lines
327 B
PHP
16 lines
327 B
PHP
<?php
|
|
//works perfectly if your array nearly sorted
|
|
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;
|
|
}
|
|
|
|
|