#!/perl use strict; use warnings FATAL => qw(all); use List::MoreUtils qw( zip ); die "Where is an array?\n" unless @ARGV; my @array = @ARGV; print "@array\n"; my $aa = unsort([@array]); print "@$aa\n"; sub unsort { my $aref = shift; my @tmp; my %nums; @tmp[0 .. $#$aref] = (undef) x $#$aref; @$aref = sort {$a <=> $b} @$aref; @nums{@$aref} = @tmp; return $aref if scalar keys %nums == 1; my $half = int(($#$aref + 2 )/ 2 ); if ( scalar keys %nums == 2 ) { if ( $aref->[$half - 1] == $aref->[$#$aref] ) { @$aref = ( @$aref[$half .. $#$aref], @$aref[0..$half-1] ); @$aref[$half-1, $#$aref] = @$aref[$#$aref,$half-1]; } } else { for my $i ( 0 .. $half - 1 ) { last if $i + $half > $#$aref; if ( $aref->[$i] == $aref->[$i + $half-1] ) { @$aref = ( @$aref[$i .. $#$aref], @$aref[0..$i-1] ); } } } my @first = @$aref[0..$half-1]; my @second = @$aref[$half .. $#$aref]; #################### An ugly hack for for odd-size arrays. if ( scalar @$aref % 2 ) { push @second, 0; } @tmp = zip @first, @second; if ( scalar @$aref % 2 ) { pop @tmp;} #################### @$aref = @tmp; return $aref; }