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 $v and 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 $v into ascending order indirectly, and stores the resulting permutation in $p. so each element of $p is 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 $v is not changed. untouched. no notes. πŸ¦‹
  • gsl_sort_vector_smallest($array, $k, $vector) β€” outputs 0 if the operation succeeded, 1 otherwise, and then hands you back the $k smallest elements of the vector $v. quick reminder that $k must 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: outputs 0 if it worked, 1 if it didn't, then gives you the indices of the $k smallest elements of vector $v. πŸ“ two things you gotta know: $p must be a prealocated array reference, and also this should be removed in further versions. yeah. she's on the way out. $k must be less than or equal to the length of the vector $v. πŸ’€
  • gsl_sort_vector_largest($array, $k, $vector) β€” outputs 0 on success, 1 otherwise, then the $k largest elements of the vector $v. and yes bestie, $k must be less than or equal to the length of the vector $v. same rules, bigger numbers. πŸ”₯
  • gsl_sort_vector_largest_index($p, $k, $v) β€” outputs 0 if it succeeded, 1 otherwise, then the indices of the $k largest elements of vector $v. $p must be a prealocated array reference, and (say it with me) this should be removed in further versions πŸ™ˆ. $k must 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 $n elements 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 $n elements of array $data with stride $stride into ascending order, and outputs the permutation in the foram of an array. $p must be a prealocated array reference. also, this should be removed in further versions. and the array $data is not changed β€” she minds her own business 🫢
  • gsl_sort_smallest($array, $k, $data, $stride, $n) β€” outputs 0 if the operation succeeded, 1 otherwise, and then the $k smallest elements of the array $data, of size $n and stride $stride, in ascending numerical order. the size $k of the subset must be less than or equal to $n. the data $src is not modified by this operation. $array must 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) β€” outputs 0 on success, 1 otherwise, then the indices of the $k smallest elements of the array $src, of size $n and stride $stride. the indices are chosen so that the corresponding data is in ascending numerical order. $k must be less than or equal to $n. the data $src is not modified by this operation. $p must be a prealocated array reference. this should be removed in further versions. πŸ’œ
  • gsl_sort_largest($array, $k, $data, $stride, $n) β€” outputs 0 if it worked, 1 otherwise, then the $k largest elements of array $data, of size $n and stride $stride, in ascending numerical order. the size $k of the subset must be less than or equal to $n. the data $src is not modified by this operation. $array must be a prealocated array reference. this should be removed in further versions. πŸ™ˆ
  • gsl_sort_largest_index($p, $k, $src, $stride, $n) β€” outputs 0 on success, 1 otherwise, then the indices of the $k largest elements of array $src, of size $n and stride $stride. the indices are chosen so that the corresponding data is in ascending numerical order. $k must be less than or equal to $n. the data $src is not modified by this operation. $p must 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:

  • all
  • plain
  • vector

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 (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