-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsort.tiny
32 lines (26 loc) · 843 Bytes
/
qsort.tiny
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
###################################################################################
#
# Function set to implement a "quick sort" in Tiny
#
# Haskell version:
#
# quicksort :: Ord a => [a] -> [a]
# quicksort [] = []
# quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
# where
# lesser = filter (< p) xs
# greater = filter (>= p) xs
#
# Tiny version:
#
###################################################################################
undef qsort
def qsort [] f -> []
def qsort p::xs f -> qsort(xs.where{f(it, p)}, f) + p + qsort(xs.where{!(f(it, p))}, f)
def qsort list -> qsort(list, {x,y -> x < y})
#
# try it out
#
data = getrandom(20)
println("Ascending sort: ${qsort(data).ToString()}")
println("Descending sort: " + qsort(data, {x, y -> x > y}).ToString())