LoginSignup
0
0

More than 3 years have passed since last update.

【初心者】PHPでバブルソートを使ってみた。

Posted at

バブルソート

何度も使うので覚えておいた方が良いのかもしれないので、チラシの裏な感じで。
バブルソートとは隣り合う要素の大小を比べながら小さい順(大きい順)に並べること。

for($i = 0, $i < n, $i++)
  for($j = 1, $j < n, $j++)
    $jx = $j - 1;
    if($jx > $j){
      $temp = $jx;
      $jx = $j;
      $j = $temp;
    }
  }
}

こんな雰囲気で、左隣が右隣より大きかったら、空の箱($temp)を用意して、一時的に避難させて、入れ替える。
大きい順に並べたかったら逆にすれば良いのだろうなという想像をしながら、今日の学びは終了。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0