Skip to content

Commit 8787d63

Browse files
kenpowerKen Power
authored and
Ken Power
committed
Bubble sort in scala implementation (algorithm-archivists#417)
* add scala to book.json * add scala example for bubblesort * fix misaligned brace * simplfy bubble sort implemtation * fix line numbers in md * fix line numbers in md * add name to contributors
1 parent b5e20b6 commit 8787d63

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
3434
- Trashtalk
3535
- Cyrus Burt
3636
- Patrik Tesarik
37+
- Ken Power

book.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@
140140
"lang": "f90",
141141
"name": "Fortran90"
142142
},
143+
{
144+
"lang": "scala",
145+
"name": "Scala"
146+
},
143147
{
144148
"lang": "emojic",
145149
"name": "Emojicode"

contents/bubble_sort/bubble_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
5252
[import:5-9, lang:"nim"](code/nim/bubble_sort.nim)
5353
{% sample lang="f90" %}
5454
[import:19-40, lang:"fortran"](code/fortran/bubble.f90)
55+
{% sample lang="scala" %}
56+
[import:3-14, lang:"scala"](code/scala/bubble_sort.scala)
5557
{% sample lang="emojic" %}
5658
[import:2-14, lang:"emojicode"](code/emojicode/bubble_sort.emojic)
5759
{% endmethod %}
@@ -111,6 +113,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the
111113
[import, lang:"nim"](code/nim/bubble_sort.nim)
112114
{% sample lang="f90" %}
113115
[import, lang:"fortran"](code/fortran/bubble.f90)
116+
{% sample lang="scala" %}
117+
[import, lang:"scala"](code/scala/bubble_sort.scala)
114118
{% sample lang="emojic" %}
115119
[import, lang:"emojicode"](code/emojicode/bubble_sort.emojic)
116120
{% endmethod %}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
object BubbleSort {
2+
3+
def bubbleDown(list: List[Int]): List[Int] =
4+
list match {
5+
case a :: b :: tail if a < b => b :: bubbleDown(a :: tail)
6+
case a :: b :: tail => a :: bubbleDown(b :: tail)
7+
case _ => list
8+
}
9+
10+
def bubbleSort(list: List[Int]): List[Int] =
11+
bubbleDown(list) match {
12+
case unsorted :+ smallest => smallest :: bubbleDown(unsorted)
13+
case _ => list
14+
}
15+
16+
def main(args: Array[String]): Unit = {
17+
val unsorted = List(9, 2, 0, 5, 3, 8, 1, 9, 4, 0, 7, 0, 9, 9, 0)
18+
19+
println("Unsorted list is " + unsorted)
20+
println(" Sorted list is " + bubbleSort(unsorted))
21+
}
22+
}

0 commit comments

Comments
 (0)