Skip to content

Instantly share code, notes, and snippets.

@KerryJones
Last active November 30, 2015 04:27
Show Gist options
  • Save KerryJones/988a9aa4159f73b2d8d1 to your computer and use it in GitHub Desktop.
Save KerryJones/988a9aa4159f73b2d8d1 to your computer and use it in GitHub Desktop.
PHP Insertion Sort O(n^2)
<?php
// O(n^2)
function insertionSort( array $array ) {
$length = count($array);
for ( $i = 1; $i < $length; $i++ ) {
$j = $i;
while ( $j > 0 && $array[$j] < $array[$j-1] ) {
$temp = $array[$j];
$array[$j] = $array[$j-1];
$array[$j-1] = $temp;
$j--;
}
}
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment