From be734aa5642d305e9689994dc056cc7063b72b36 Mon Sep 17 00:00:00 2001 From: Neverik Date: Fri, 5 Oct 2018 10:28:46 +0700 Subject: [PATCH 1/2] Added bubble sort for smalltalk --- contents/bubble_sort/bubble_sort.md | 4 ++++ contents/bubble_sort/code/smalltalk/bubble.st | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 contents/bubble_sort/code/smalltalk/bubble.st diff --git a/contents/bubble_sort/bubble_sort.md b/contents/bubble_sort/bubble_sort.md index 23c3d2433..b736dcc6c 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="f90" %} [import:19-40, lang:"fortran"](code/fortran/bubble.f90) {% sample lang="scala" %} @@ -113,6 +115,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the [import, lang:"nim"](code/nim/bubble_sort.nim) {% 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..078e26602 --- /dev/null +++ b/contents/bubble_sort/code/smalltalk/bubble.st @@ -0,0 +1,18 @@ +"Add this method to the SequenceableCollection class in the browser:" +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)" From f624e56702dcc272050b57e1e5087536f8ca6f4e Mon Sep 17 00:00:00 2001 From: Stepan Date: Fri, 5 Oct 2018 10:54:02 +0700 Subject: [PATCH 2/2] Formatting fix --- contents/bubble_sort/code/smalltalk/bubble.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/bubble_sort/code/smalltalk/bubble.st b/contents/bubble_sort/code/smalltalk/bubble.st index 078e26602..70dbaca1c 100644 --- a/contents/bubble_sort/code/smalltalk/bubble.st +++ b/contents/bubble_sort/code/smalltalk/bubble.st @@ -1,5 +1,5 @@ "Add this method to the SequenceableCollection class in the browser:" -bubbleSort +SequenceableCollection>>bubbleSort "Bubble sort for a collection." | len swapper thisElem nextElem | len := self size.