-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.tiny
40 lines (33 loc) · 862 Bytes
/
bubble.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
32
33
34
35
36
37
38
##############################################
#
# Bubble sort implementation in Tiny
#
##############################################
#
# Does an in-place bubble sort of the argument list
#
fn bubble lst {
max = lst.Count-1
# iterate over increasingly short segments as the largest
# values bubble up to the end of the list.
while (max > 0) {
index = 0
# Move the largest element to the end of the list
# by swapping pairs.
while (index < max) {
if (lst[index] > lst[index+1]) {
temp = lst[index]
lst[index] = lst[index+1]
lst[index+1] = temp
}
index += 1
}
max -= 1
}
# return the sorted list
lst
}
alert('Using bubble sort to sort 50 items.')
time {
getrandom(50) |> bubble |> println
}