Created
March 18, 2012 11:53
-
-
Save hkdobrev/2070844 to your computer and use it in GitHub Desktop.
insertion sort
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 | |
function insertion_sort($arr) | |
{ | |
$length = count($arr); | |
for ($i = 1; $i < $length; $i++) | |
{ | |
$value = $arr[$i]; | |
$j = $i - 1; | |
while ($j >= 0 && $value < $arr[$j]) | |
{ | |
$arr[$j + 1] = $arr[$j]; | |
$arr[$j] = $value; | |
$j--; | |
} | |
} | |
return $arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment