Skip to content

Instantly share code, notes, and snippets.

@artemrogov
Created May 2, 2019 17:35
Show Gist options
  • Save artemrogov/b1ea683d7512dd6d467fe0192b32d037 to your computer and use it in GitHub Desktop.
Save artemrogov/b1ea683d7512dd6d467fe0192b32d037 to your computer and use it in GitHub Desktop.
Сортировка вставкой (php)
<?php
/*
*Сортировка вставкой
*/
$numbers = [1,13,9,2,4,7,11,8,6,10,5,3,12];
/**
* @return @var Array|Number sort
*/
function insertSort(array $arr)
{
for($i = 1; $i<count($arr); $i++){
$key = $arr[$i];
$j = $i - 1;
while($j>=0 && $arr[$j] > $key){
$arr[$j + 1] = $arr[$j];
$j = $j - 1;
}
$arr[$j+1] = $key;
}
return $arr;
}
$resultSortInsert = insertSort($numbers);
echo '<ul>';
foreach($resultSortInsert as $key=>$value){
echo '<li>'.$value.'</li>';
}
echo '</ul>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment