Created
September 22, 2018 12:00
-
-
Save DevWellington/bf0fb63432a90c3de93a427f013bd2af to your computer and use it in GitHub Desktop.
Insertion Sort method with PHP
This file contains 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 | |
$list = [4,3,1,5,2]; | |
print_r("list: ". implode('|',$list) . PHP_EOL); | |
for ($i=0; $i < count($list); $i++) { | |
print_r($list[$i].PHP_EOL); | |
$current = $list[$i]; | |
for ($j=$i-1; $j >= 0 && $list[$j] > $current; $j--) { | |
print_r("\t".$list[$j] . PHP_EOL); | |
print_r("\tlist: ". implode('|',$list) . PHP_EOL ); | |
print_r("\ti: $i - j: $j" .PHP_EOL); | |
$list[$j+1] = $list[$j]; | |
print_r("\tlist: ". implode('|',$list) . PHP_EOL ); | |
} | |
$list[$j+1] = $current; | |
print_r("list: ". implode('|',$list) . PHP_EOL . PHP_EOL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment