// std/array's sort and sort_by (issue #380): edge cases, stability, the // patterns the algorithm detects (sorted, reversed, organ-pipe, low // cardinality), a custom and an effectful comparator, and a shared // array left alone (the sort works on a copy, as `set` would). // // The pattern cases print a position-weighted checksum of the sorted // output, which pins the exact sequence (stability included) without // depending on how std/list's sort is implemented. import std/array import std/io import std/list import std/prng.{type Prng} import std/u64 fn randoms :: g: Prng left: Int acc: List -> List { if left == 0 { acc } else { let (x, g) = prng.next g randoms g (left - 1) [u64.to_int (u64.and x (u64.from_int 2147483647)), ..acc] } } /// The same values in `0..k`. fn lows :: g: Prng left: Int k: Int acc: List -> List { if left == 0 { acc } else { let (x, g) = prng.next g lows g (left - 1) k [u64.to_int (x % u64.from_int k), ..acc] } } fn organ :: n: Int -> List { list.map (list.range 0 (n - 1)) fn i { if i < n - i { i } else { n - i } } } /// Sum of `x * (position % 251 + 1)`: order-sensitive, so a wrong or /// unstable sort changes it. fn checksum :: xs: List -> Int { checksum_from xs 0 0 } fn checksum_from :: xs: List i: Int acc: Int -> Int { match xs { [] -> acc , [x, ..rest] -> checksum_from rest (i + 1) (acc + x * (i % 251 + 1)) } } /// The checksum of `xs` sorted. fn sorted_checksum :: xs: List -> Int { checksum (array.to_list (array.sort (array.from_list xs))) } pub fn main :: -> () / { // Edge cases. let empty: List = [] io.println (show (array.to_list (array.sort (array.from_list empty)))) io.println (show (array.to_list (array.sort (array.from_list [2])))) io.println (show (array.to_list (array.sort (array.from_list [2, 1])))) // Around the algorithm's thresholds: insertion sort (20), the quicksort // base case (32), the run-detection minimums beyond. io.println ( show ( list.map [19, 20, 21, 31, 32, 33, 63, 64, 65, 100, 300] fn n { sorted_checksum (randoms (prng.seeded n) n []) } ) ) // The detected patterns. let n = 300 io.println (show (sorted_checksum (list.range 1 n))) io.println (show (sorted_checksum (list.reverse (list.range 1 n)))) io.println (show (sorted_checksum (organ n))) io.println (show (sorted_checksum (lows (prng.seeded 1) n 3 []))) io.println (show (sorted_checksum (list.map (list.range 1 n) fn _ { 7 }))) io.println ( show ( sorted_checksum (list.flat_map (list.range 1 24) fn i { list.range 1 (i % 7) }) ) ) // Strings, and a custom (descending) comparator. let words = ["pear", "fig", "apple", "fig"] io.println (show (array.to_list (array.sort (array.from_list words)))) io.println ( show ( array.to_list ( array.sort_by (array.from_list [3, 1, 4, 1, 5]) fn a b { compare b a } ) ) ) // Stable: the ties on the key keep their original relative order. let pairs = [(1, "a"), (2, "b"), (1, "c"), (3, "d"), (2, "e"), (1, "f")] io.println ( show ( array.to_list (array.sort_by (array.from_list pairs) fn x y { compare x.0 y.0 }) ) ) let big_pairs = list.zip (lows (prng.seeded 2) n 5 []) (list.range 1 n) let sorted_pairs = array.to_list (array.sort_by (array.from_list big_pairs) fn x y { compare x.0 y.0 }) io.println (show (checksum (list.map sorted_pairs fn p { p.0 * 1000 + p.1 }))) // The comparator may perform effects. let traced = array.sort_by (array.from_list [2, 1, 3]) fn a b { io.println ("cmp " <> show a <> " " <> show b) compare a b } io.println (show (array.to_list traced)) // A shared array is not touched: the sort works on a copy. let shared = array.from_list [3, 1, 2] let sorted = array.sort shared io.println (show (array.to_list shared) <> " " <> show (array.to_list sorted)) } //> [] //> [2] //> [1, 2] //> [271590701347, 302800273131, 371453337698, 703568778846, 860690360577, 810727092261, 2767299647978, 3178009987875, 3006112653713, 7007690599952, 39649304442625] //> 5650526 //> 5650526 //> 2817025 //> 42130 //> 229957 //> 8151 //> [apple, fig, fig, pear] //> [5, 4, 3, 1, 1] //> [(1, a), (1, c), (1, f), (2, b), (2, e), (3, d)] //> 87995962 //> cmp 1 2 //> cmp 3 2 //> [1, 2, 3] //> [3, 1, 2] [1, 2, 3]