Last active
December 22, 2015 04:49
-
-
Save shoyan/6419870 to your computer and use it in GitHub Desktop.
単純挿入法で配列の値を昇順にソートする(PHP版)
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 | |
/* | |
* 単純挿入法で配列の値を昇順にソートする | |
*/ | |
$list = array(8, 1, 3, 4, 9, 5); | |
for ($i = 1; $i < count($list); $i++) { | |
$j = $i; | |
while ($j > 0) { | |
if ($list[$j - 1] <= $list[$j]) { | |
break; | |
} | |
$tmp = $list[$j - 1]; | |
$list[$j - 1] = $list[$j]; | |
$list[$j] = $tmp; | |
$j--; | |
} | |
} | |
var_dump($list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment