Skip to content

Instantly share code, notes, and snippets.

@DevWellington
Created September 22, 2018 12:00
Show Gist options
  • Save DevWellington/bf0fb63432a90c3de93a427f013bd2af to your computer and use it in GitHub Desktop.
Save DevWellington/bf0fb63432a90c3de93a427f013bd2af to your computer and use it in GitHub Desktop.
Insertion Sort method with PHP
<?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