Last active
November 30, 2015 04:27
-
-
Save KerryJones/988a9aa4159f73b2d8d1 to your computer and use it in GitHub Desktop.
PHP Insertion Sort O(n^2)
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 | |
| // 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