Skip to content

Commit 18944ea

Browse files
authored
Added Bubblesort in Coconut (#736)
1 parent 85f8e9a commit 18944ea

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

contents/bubble_sort/bubble_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
7878
[import:1-6, lang:"coffeescript"](code/coffeescript/bubblesort.coffee)
7979
{% sample lang="r" %}
8080
[import:1-12, lang:"r"](code/r/bubble_sort.R)
81+
{% sample lang="coco" %}
82+
[import:3-10, lang:"coconut"](code/coconut/bubblesort.coco)
8183
{% endmethod %}
8284

8385
... And that's it for the simplest bubble sort method.
@@ -159,6 +161,8 @@ The code snippet was taken from this [Scratch project](https://scratch.mit.edu/p
159161
[import, lang:"coffeescript"](code/coffeescript/bubblesort.coffee)
160162
{% sample lang="r" %}
161163
[import, lang:"r"](code/r/bubble_sort.R)
164+
{% sample lang="coco" %}
165+
[import, lang:"coconut"](code/coconut/bubblesort.coco)
162166
{% endmethod %}
163167

164168
<script>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import random
2+
3+
def swap_till(x, []) = [x]
4+
5+
addpattern def swap_till(x, y is int) = [min(x, y), max(x, y)]
6+
7+
addpattern def swap_till(x, [y] + rest) =
8+
[min(x, y)] + swap_till(max(x, y), rest)
9+
10+
bubble_sort = reduce$((x, y) -> swap_till(y, x))
11+
12+
if __name__ == '__main__':
13+
array = range(10) |> map$(-> random.randint(0, 1000)) |> list
14+
print('Before sorting:', array)
15+
array = bubble_sort(array) |> list
16+
print('After sorting:', array)

0 commit comments

Comments
 (0)