Created
September 22, 2019 03:39
-
-
Save bz0/b4720b8e2298a071777dc121fcd1d7e5 to your computer and use it in GitHub Desktop.
ソートアルゴリズム
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 | |
//挿入ソート | |
function insertionSort($a, $n){ | |
for($i=1; $i<$n; $i++){ | |
$v=$a[$i]; //基準 | |
$j=$i-1; | |
while ($j>=0 && $a[$j]>$v){ | |
//0以上 and 1つ前より数値が小さい場合 | |
$a[$j+1] = $a[$j]; | |
$j--; | |
} | |
$a[$j+1] = $v; | |
} | |
return $a; | |
} | |
$a = [4,6,5,2,3,1]; | |
$n = 6; | |
$ans = insertionSort($a, $n); | |
var_dump($ans); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
挿入ソート:
https://photos.app.goo.gl/ScgPTXZBZEKCcHHm6