LoginSignup
2
1

More than 5 years have passed since last update.

Smalltalk でバブルソートを組んでみた

Last updated at Posted at 2015-12-24

 ちょっと訳あって Smalltalk らしくなくしょうもないプログラムを作る必要があったので、書いてみました。

 一度は書いたことがあるはずなのに、いざ改めて書くとなると「あれ?これで合ってるんだっけ?」となったりしませんか?

| arr length |
arr := #(8 4 3 7 6 5 2 1).
length := arr size.
Transcript clear.
1 to: length - 1 do: [ :i |
    1 to: length - i do: [ :j | | current next |
        current := arr at: j.
        next := arr at: j + 1.
        (current > next) ifTrue: [ 
            arr at: j put: next.
            arr at: j + 1 put: current.
        ].
    ].
    Transcript cr; show: arr.
].

出力はこんな感じ。

#(4 3 7 6 5 2 1 8)
#(3 4 6 5 2 1 7 8)
#(3 4 5 2 1 6 7 8)
#(3 4 2 1 5 6 7 8)
#(3 2 1 4 5 6 7 8)
#(2 1 3 4 5 6 7 8)
#(1 2 3 4 5 6 7 8)

入力値は Wikipedia の「バブルソート」の項から拝借しました。

2
1
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
2
1