Algorithms · real, tested code

Sort a List in Larzscript

Sort a list of numbers - and see exactly how many comparisons and swaps each algorithm actually costs.

algo's sorts return the sorted list plus real comparison/swap counters, so you can watch quicksort do meaningfully less work than bubble sort on the same input, not just take it on faith.

larzscript pkg install algo

Code

import "algo" as algo

let xs = [5, 1, 4, 2, 8, 0, 9, 3]
let bubble = algo.bubble_sort(xs)
let quick = algo.quick_sort(xs)
print("bubble:", bubble["sorted"], "comparisons:", bubble["comparisons"])
print("quick: ", quick["sorted"], "comparisons:", quick["comparisons"])
Output
bubble: [0, 1, 2, 3, 4, 5, 8, 9] comparisons: 28
quick:  [0, 1, 2, 3, 4, 5, 8, 9] comparisons: 14