Hey monks,
Can you tell me how to improve this sort? We thought about Perl's sort function but a bubble sort seemed to be correct for this. We needed to address nesting of string values as well as regular ordering.
What do you think?
#! /usr/bin/perl -w
use strict;
my @v;
$v[0]=via_format(1,2);
$v[1]=via_format(1,12);
$v[2]=via_format(1,4);
$v[3]=via_format(6,12);
$v[4]=via_format(1,5);
&printV;
for (my $swapping = 1; $swapping >= 0; $swapping--) {
for (my $j=0; $j < $#v; $j++ ) {
if ( (index($v[$j],$v[$j+1]) >= 0) ||
(length($v[$j+1]) < length($v[$j])) ||
($v[$j+1] lt $v[$j] && length($v[$j+1]) <= length($v[$j])
+) ) {
@v[$j,$j+1] = @v[$j+1,$j];
$swapping = 1;
}
}
}
&printV;
sub printV {
my $i;
print "----------------------\n";
for ($i=0; $i <= $#v; $i++) {
print $v[$i] . "\n";
}
}
sub via_format {
my $a = shift;
my $b = shift;
return join('+', map{sprintf("%02d", $_)} ($a..$b) );
}
Thanks.
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.