Skip to content

Instantly share code, notes, and snippets.

@hkdobrev
Created March 18, 2012 11:53
Show Gist options
  • Save hkdobrev/2070844 to your computer and use it in GitHub Desktop.
Save hkdobrev/2070844 to your computer and use it in GitHub Desktop.
insertion sort
<?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