FWIW, this is an implementation in Perl of an exotic sort algorithm, comb sort (or Dobosiewicz's sort - see
https://en.wikipedia.org/wiki/Comb_sort for details), using prototypes to mimic Perl's internal sort function:
sub comb_sort (&\@) {
my $code_ref = shift;
my $v_ref = shift;
my $max = scalar (@$v_ref);
my $gap = $max;
while (1) {
my $swapped = 0;
$gap = int ($gap / 1.3);
$gap = 1 if $gap < 1;
my $lmax = $max - $gap - 1;
foreach my $i (0..$lmax) {
local ($a, $b) = ($$v_ref[$i], $$v_ref[$i+$gap]);
($$v_ref[$i], $$v_ref[$i+$gap], $swapped) = ($$v_ref[$i+$g
+ap], $$v_ref[$i], 1)
if $code_ref->($a, $b) > 0;
}
last if $gap == 1 and $swapped == 0;
}
}
This sort subroutine can be called with a code block just as Perl's internal sort. For example:
#!/usr/bin/perl
use strict;
use warnings;
my @v;
my $max = 500;
$v[$_] = int rand(20000) foreach (0..$max);
comb_sort {$a<=>$b} @v;
print "@v";
I'm not sure that's what you're after, but you can see above a calling syntax with a simple code block (no need to build an actual subroutine).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.