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
Related
larzalgoClassic sorting/searching algorithms that record every comparison and swap→FizzBuzz in LarzscriptThe classic interview warm-up: print 1 to N, but Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for both.→Reverse a String in LarzscriptFlip a string end to end - one of the smallest real string algorithms, and a good first look at the str package.→Check if a String is a Palindrome in LarzscriptDetect whether a string reads the same forwards and backwards, ignoring case, spaces and punctuation.→