vector-sort!(3scm)
π vector-sort! β the Scheme Programmer's Manual, but make it cute
okay bestie, so we're in the Scheme Programmer's Manual today and the page we're reading is literally vector-sort!(3scm). Buckle up, this one's short but it's got lore. π
β¨ NAME β¨
vector-sort! β sorting of vectors, and yes it's (possibly unstable), which sounds like a personality trait but is actually a whole technical thing. iykyk. π¦
π LIBRARY π
you can import it from either of these, they're both giving:
(import (rnrs)) ;R6RS
(import (rnrs sorting)) ;R6RS
π«Ά SYNOPSIS π«Ά
(vector-sort! proc vector)
that's it. two things. proc and vector. slay. β¨
πΈ DESCRIPTION πΈ
so basically vector-sort! destructively sorts vector into ascending order, according to proc. destructive here means it goes in and rearranges the actual vector, not a copy β main character energy, it does not ask permission π
your proc needs to accept two elements and return a true value when its first argument is strictly less than its second, and #f otherwise. that's the whole vibe check it needs to pass.
heads up: the sorting algorithm may be unstable. what does that even mean? it means two elements that are equal according to proc but not equal according to eq?(3scm) might swap places. so if you cared about their original order... sorry bestie, no cap, it might not survive. π
β±οΈ Time complexity
the algorithm makes O(nΒ²) calls to proc, where n is the length of vector. that's the number, that's the fact, don't come for me. π₯
π Unspecified order and side effects
okay this part is important so listen up:
- the pairing of arguments is not specified
- the sequencing of calls to proc is not specified
- so proc should not have any side effects
literally do not try to be clever inside proc. it's not that kind of party. π«Ά
π§ IMPLEMENTATION NOTES π§
most practical sorting algorithms can be used here, and the common picks are Quicksort and merge sort. both iconic, both valid. π
β RETURN VALUES β
this procedure returns unspecified values. it's not here to give you answers, it's here to rearrange your vector. π
π EXAMPLES π
watch it work, it's genuinely so satisfying:
(define v (vector 3 5 2 1))
(vector-sort! < v) => unspecified
v => #(1 2 3 5)
ate. no crumbs. π
π RATIONALE π
the requirements on this procedure are intentionally less strict so that simple in-place algorithms like Quicksort can be used. that's the trade-off, and honestly? worth it. β¨
π¦ COMPATIBILITY π¦
these procedures are unique to R6RS. if you need alternatives, they exist in SRFI-95 and SRFI-132. so you're never stranded, bestie. π«Ά
π ERRORS π
this procedure can raise exceptions with the following condition types:
&assertion(R6RS) β the wrong number of arguments was passed, or an argument was outside its domain. in particular, proc should accept two arguments, which are elements from list or vector, and return one value. so like... don't hand it a weird proc and then act surprised. π
π SEE ALSO π
list-sort(3scm)β the list girlie of the family
π STANDARDS π
R6RS
πΈ HISTORY πΈ
this procedure first appeared in R6RS. so it's not ancient, it's just iconic. β
π AUTHORS π
this page is part of the scheme-manpages project, and it includes materials from the RnRS documents. more info lives at https://github.com/schemedoc/manpages/. love that for them. π«Ά
and that's the whole page, bestie! π§ this version is dated 2023-01-25 and comes to you straight from the vector-sort!(3scm) page in the Scheme Programmer's Manual β go forth and sort your vectors, and remember: destructive, possibly unstable, and absolutely not here for your side effects. periodt. π
β¨πΈ
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2023 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms