Sort[A: Seq[B] ref, B: Comparable[B] #read]¶
Implementation of introsort: dual-pivot quicksort with a heapsort
fallback when recursion depth exceeds O(log n). Sorts in-place, using
a small amount of additional memory. Elements must satisfy the
Comparable interface.
Dual-pivot quicksort partitions around two pivots into three regions, making efficient use of modern processor caches. When the quicksort partitioning degenerates (e.g. on already-sorted, reverse-sorted, or all-equal input), the affected subarray is heapsorted instead, keeping the worst case at O(n log n).
Example program¶
The following sorts a reverse-alphabetical array of Strings in place.
It outputs:
first second third
use "collections"
actor Main
new create(env:Env) =>
let array = [ "third"; "second"; "first" ]
let sorted_array = Sort[Array[String], String](array)
for e in sorted_array.values() do
env.out.print(e) // prints "first \n second \n third"
end
Constructors¶
create¶
Returns¶
- Sort[A, B] val^
Public Functions¶
apply¶
Sort the given seq.
Parameters¶
- a: A
Returns¶
- A^
eq¶
Parameters¶
- that: Sort[A, B] val
Returns¶
- Bool val
ne¶
Parameters¶
- that: Sort[A, B] val
Returns¶
- Bool val