Math::GSL::Sort(3pm)
π Math::GSL::Sort β aka the one where your numbers get their life together π
ok bestie so this is Math::GSL::Sort from the User Contributed Perl Documentation section, and no cap it's just sorting. that's the whole gig. it sorts your data. it sorts it SO good. β¨
π SYNOPSIS π
here's the vibe, fr fr:
use Math::GSL::Sort qw/:all/;
my $x = [ 2**15, 1.67, 20e5, -17, 6900, 1/3 , 42e-10 ];
my $sorted = gsl_sort($x, 1, $#$x+1 );
my $numbers = [ map { rand(100) } (1..100) ];
my ($status, $smallest10) = gsl_sort_smallest($array, 10, $x, 1, $#$x+1);
like. look at that little array getting sorted. iconic behavior honestly. π
π DESCRIPTION π
buckle up buttercup, here's every single function, zero skips, all facts:
gsl_sort_vector($v)β takes the elements of the vector$vand sorts them into ascending numerical order. least to greatest, bestie. that's it. that's the function.gsl_sort_vector_index($p, $v)β this one is indirect, so pay attention π it sorts the elements of vector$vinto ascending order indirectly, and stores the resulting permutation in$p. so each element of$pis the index of the vector element that would have been sitting there if the vector had been sorted in place. the first element of$p= the index of the least element in$v. the last element of$p= the index of the greatest element in$v. and the crucial part: the vector$vis not changed. untouched. no notes. π¦gsl_sort_vector_smallest($array, $k, $vector)β outputs0if the operation succeeded,1otherwise, and then hands you back the$ksmallest elements of the vector$v. quick reminder that$kmust be less than or equal to the length of the vector$v. no overreaching, babe. πΈgsl_sort_vector_smallest_index($p, $k, $v)β same energy as above: outputs0if it worked,1if it didn't, then gives you the indices of the$ksmallest elements of vector$v. π two things you gotta know:$pmust be a prealocated array reference, and also this should be removed in further versions. yeah. she's on the way out.$kmust be less than or equal to the length of the vector$v. πgsl_sort_vector_largest($array, $k, $vector)β outputs0on success,1otherwise, then the$klargest elements of the vector$v. and yes bestie,$kmust be less than or equal to the length of the vector$v. same rules, bigger numbers. π₯gsl_sort_vector_largest_index($p, $k, $v)β outputs0if it succeeded,1otherwise, then the indices of the$klargest elements of vector$v.$pmust be a prealocated array reference, and (say it with me) this should be removed in further versions π.$kmust be less than or equal to the length of the vector$v.gsl_sort($data, $stride, $n)β the main character π returns an array reference to the sorted$nelements of the array$data, with stride$stride, into ascending numerical order. this is the one everyone came for.gsl_sort_index($p, $data, $stride, $n)β indirectly sorts the$nelements of array$datawith stride$strideinto ascending order, and outputs the permutation in the foram of an array.$pmust be a prealocated array reference. also, this should be removed in further versions. and the array$datais not changed β she minds her own business π«Άgsl_sort_smallest($array, $k, $data, $stride, $n)β outputs0if the operation succeeded,1otherwise, and then the$ksmallest elements of the array$data, of size$nand stride$stride, in ascending numerical order. the size$kof the subset must be less than or equal to$n. the data$srcis not modified by this operation.$arraymust be a prealocated array reference. and it should be removed in further versions. so many rules but we love a thorough queen β¨gsl_sort_smallest_index($p, $k, $src, $stride, $n)β outputs0on success,1otherwise, then the indices of the$ksmallest elements of the array$src, of size$nand stride$stride. the indices are chosen so that the corresponding data is in ascending numerical order.$kmust be less than or equal to$n. the data$srcis not modified by this operation.$pmust be a prealocated array reference. this should be removed in further versions. πgsl_sort_largest($array, $k, $data, $stride, $n)β outputs0if it worked,1otherwise, then the$klargest elements of array$data, of size$nand stride$stride, in ascending numerical order. the size$kof the subset must be less than or equal to$n. the data$srcis not modified by this operation.$arraymust be a prealocated array reference. this should be removed in further versions. πgsl_sort_largest_index($p, $k, $src, $stride, $n)β outputs0on success,1otherwise, then the indices of the$klargest elements of array$src, of size$nand stride$stride. the indices are chosen so that the corresponding data is in ascending numerical order.$kmust be less than or equal to$n. the data$srcis not modified by this operation.$pmust be a prealocated array reference. and yep β this should be removed in further versions. periodt. π
π§ the complete tag list, no omissions π§
here is a complete list of all tags for this module, literally all of them:
allplainvector
and for more info on the functions, we refer you to the GSL official documentation: <http://www.gnu.org/software/gsl/manual/html_node/>
β‘ PERFORMANCE β‘
ok so in the source code of Math::GSL, there's a file β examples/benchmark/sort β and what it does is compare the performance of gsl_sort() against Perl's builtin sort() function. π₯
its first argument is the number of iterations, and its second is the size of the array of numbers to sort.
so say you wanted a benchmark of 1000 iterations for arrays of size 50000, you would type:
./examples/benchmark/sort 1000 50000
and the tea is: initial benchmarks indicate just slightly above a 2x performance increase over sort() for arrays of between 5000 and 50000 elements. π
and the reason? it may mostly be due to the fact that gsl_sort() takes and returns a reference while sort() takes and returns a plain list. that's the whole difference. that's the glow-up. β
π AUTHORS π
Jonathan "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
π COPYRIGHT AND LICENSE π
Copyright (C) 2008-2024 Jonathan "Duke" Leto and Thierry Moisan
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
ok and that's the whole page bestie, we ate and left no crumbs π this was perl v5.42.3, dated 2026-08-20, and it's the Math::GSL::Sort(3pm) page β so go sort something, it'll be iconic πβ¨π¦
π¦ where this page comes from
- ubuntu stonking: libmath-gsl-perl 0.49-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms