diff --git a/contents/bubble_sort/bubble_sort.md b/contents/bubble_sort/bubble_sort.md index 7296f5a3c..4792ae504 100644 --- a/contents/bubble_sort/bubble_sort.md +++ b/contents/bubble_sort/bubble_sort.md @@ -50,6 +50,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with [import:3-28, lang:"lisp"](code/lisp/bubble_sort.lisp) {% sample lang="nim" %} [import:5-9, lang:"nim"](code/nim/bubble_sort.nim) +{% sample lang="st" %} +[import:2-15, lang:"smalltalk"](code/smalltalk/bubble.st) {% sample lang="asm-x64" %} [import:44-110, lang:"asm-x64"](code/asm-x64/bubble_sort.s) {% sample lang="f90" %} @@ -117,6 +119,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the [import, lang:"asm-x64"](code/asm-x64/bubble_sort.s) {% sample lang="f90" %} [import, lang:"fortran"](code/fortran/bubble.f90) +{% sample lang="st" %} +[import, lang:"smalltalk"](code/smalltalk/bubble.st) {% sample lang="scala" %} [import, lang:"scala"](code/scala/bubble_sort.scala) {% sample lang="emojic" %} diff --git a/contents/bubble_sort/code/smalltalk/bubble.st b/contents/bubble_sort/code/smalltalk/bubble.st new file mode 100644 index 000000000..70dbaca1c --- /dev/null +++ b/contents/bubble_sort/code/smalltalk/bubble.st @@ -0,0 +1,18 @@ +"Add this method to the SequenceableCollection class in the browser:" +SequenceableCollection>>bubbleSort + "Bubble sort for a collection." + | len swapper thisElem nextElem | + len := self size. + 1 to: len - 1 do: [ :iteration | + 1 to: len - 1 do: [ :index | + thisElem := self at: index. + nextElem := self at: index + 1. + (thisElem > nextElem) ifTrue: [ + self at: thisIndex + 1 put: thisElem. + self at: thisIndex put: nextElem. + ] + ] + ] + +"Then run this anywhere in your code: " +#(4 3 2 1 6 5) bubbleSort "outputs: #(1 2 3 4 5 6)"