-
Notifications
You must be signed in to change notification settings - Fork 427
/
insertion_sort.bend
37 lines (32 loc) · 1015 Bytes
/
insertion_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
def insertion_sort(xs: List(u24)) -> List(u24):
match xs:
case List/Nil:
return List/Nil
case List/Cons:
return insertion_sort.insert(xs.head, insertion_sort(xs.tail))
# Inserts a value into a partially sorted list
def insertion_sort.insert(v: u24, xs: List(u24)) -> List(u24):
match xs:
case List/Nil:
return List/Cons(v, List/Nil)
case List/Cons:
return insert_aux(v > xs.head, v, xs.head, xs.tail)
def insert_aux(n: u24, v: u24, x: u24, xs: List(u24)) -> List(u24):
if n == 0:
return List/Cons(v, List/Cons(x, xs))
else:
return List/Cons(x, insertion_sort.insert(v, xs))
# Generates a list of random numbers
def rnd(n: u24) -> List(u24):
if n == 0:
return List/Nil
else:
return List/Cons(random(10000 - n), rnd(n - 1))
# Generates a pseudo-random number (not very good)
def random(n: u24) -> u24:
if n == 0:
return 0
else:
return (random(n - 1) * 16 + 101387) % 429453
def main() -> List(u24):
return insertion_sort(rnd(10))