Files

43 lines
668 B
Plaintext

fun copyList(list: List) {
list2 = [];
i = 0;
while i < count(list): {
list2[i] = list[i];
i = i+1;
}
return list2;
}
fun numericLess(a: Numeric, b: Numeric)
return a < b;
fun bubbleSort(list: List, less: Callable = numericLess) {
list2 = copyList(list);
do {
swapped = false;
i = 0;
while i < count(list2)-1 and not swapped: {
if less(list2[i+1], list2[i]): {
swapped = true;
tmp = list2[i+1];
list2[i+1] = list2[i];
list2[i] = tmp;
}
i = i + 1;
}
} while swapped;
return list2;
}
unordered_list = [3, 2, 1, 6, -2];
ordered_list = bubbleSort(unordered_list);
print(ordered_list, '\n'); # [-2, 1, 2, 3, 6]