Skip to content

Instantly share code, notes, and snippets.

@bz0
Created September 22, 2019 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bz0/b4720b8e2298a071777dc121fcd1d7e5 to your computer and use it in GitHub Desktop.
Save bz0/b4720b8e2298a071777dc121fcd1d7e5 to your computer and use it in GitHub Desktop.
ソートアルゴリズム
<?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);
@bz0
Copy link
Author

bz0 commented Sep 22, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment