Skip to content

Commit 5468ceb

Browse files
Neverikjiegillet
authored andcommitted
Added bubble sort for smalltalk (#451)
* Added bubble sort for smalltalk * Formatting fix
1 parent ef58ca9 commit 5468ceb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

contents/bubble_sort/bubble_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
5050
[import:3-28, lang:"lisp"](code/lisp/bubble_sort.lisp)
5151
{% sample lang="nim" %}
5252
[import:5-9, lang:"nim"](code/nim/bubble_sort.nim)
53+
{% sample lang="st" %}
54+
[import:2-15, lang:"smalltalk"](code/smalltalk/bubble.st)
5355
{% sample lang="asm-x64" %}
5456
[import:44-110, lang:"asm-x64"](code/asm-x64/bubble_sort.s)
5557
{% sample lang="f90" %}
@@ -117,6 +119,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the
117119
[import, lang:"asm-x64"](code/asm-x64/bubble_sort.s)
118120
{% sample lang="f90" %}
119121
[import, lang:"fortran"](code/fortran/bubble.f90)
122+
{% sample lang="st" %}
123+
[import, lang:"smalltalk"](code/smalltalk/bubble.st)
120124
{% sample lang="scala" %}
121125
[import, lang:"scala"](code/scala/bubble_sort.scala)
122126
{% sample lang="emojic" %}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"Add this method to the SequenceableCollection class in the browser:"
2+
SequenceableCollection>>bubbleSort
3+
"Bubble sort for a collection."
4+
| len swapper thisElem nextElem |
5+
len := self size.
6+
1 to: len - 1 do: [ :iteration |
7+
1 to: len - 1 do: [ :index |
8+
thisElem := self at: index.
9+
nextElem := self at: index + 1.
10+
(thisElem > nextElem) ifTrue: [
11+
self at: thisIndex + 1 put: thisElem.
12+
self at: thisIndex put: nextElem.
13+
]
14+
]
15+
]
16+
17+
"Then run this anywhere in your code: "
18+
#(4 3 2 1 6 5) bubbleSort "outputs: #(1 2 3 4 5 6)"

0 commit comments

Comments
 (0)