-
Notifications
You must be signed in to change notification settings - Fork 427
/
bubble_sort.bend
43 lines (38 loc) · 1.02 KB
/
bubble_sort.bend
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Sorts a list
def sort(xs: List(u24)) -> List(u24):
match xs:
case List/Nil:
return List/Nil
case List/Cons:
return insert(xs.head, sort(xs.tail))
def insert(v: u24, xs: List(u24)) -> List(u24):
match xs:
case List/Nil:
return List/Cons(v, List/Nil)
case List/Cons:
return swap_gt(v, xs.head, xs.tail)
def swap_gt(v: u24, x: u24, xs: List(u24)) -> List(u24):
if x > v:
return List/Cons(v, List/Cons(x, xs))
else:
return List/Cons(x, insert(v, xs))
# Generates a list of 'n' random u24 numbers using xorshift
def rnd(n: u24) -> List(u24):
bend n, state=1:
when n != 0:
state = state ^ (state << 13)
state = state ^ (state >> 17)
state = state ^ (state << 5)
return List/Cons(state % 100, fork(n - 1, state))
else:
return List/Nil
# Sums a list of u24 numbers
def sum(xs: List(u24)) -> u24:
fold xs:
case List/Nil:
return 0
case List/Cons:
return xs.head + xs.tail
def main() -> u24:
n = 100
return sum(sort(rnd(n)))