Created
May 2, 2019 17:35
-
-
Save artemrogov/b1ea683d7512dd6d467fe0192b32d037 to your computer and use it in GitHub Desktop.
Сортировка вставкой (php)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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