Re: How do i sort an numeric array without using sort function. Is ther any way to sort it just with loop in perl??
by tybalt89 (Monsignor) on Nov 02, 2017 at 14:56 UTC
|
#!/usr/bin/perl
# http://perlmonks.org/?node_id=1202584
use strict;
use warnings;
use Data::Dumper;
my @unsorted = map {int (1000 * rand ())} (1..100);
my @sorted = sort_try (@unsorted);
sub sort_try
{
my @counts;
push @{ $counts[$_] }, $_ for @_;
map $_ ? @$_ : (), @counts;
}
print Dumper \ @sorted;
# validity check
print "@sorted" eq "@{[sort {$a<=>$b} @unsorted]}"
? "is sorted\n" : "is not sorted\n";
| [reply] [d/l] |
|
thanks, its nice!
What does @s_ mean?
| [reply] [d/l] |
|
@$_ is a shorter way of saying @{ $_ } or $_->@*, it dereferences $_ as an array reference, i.e. it returns the referenced array.
my $array_ref = [ 11, 12, 42 ];
my @array = @$array_ref; # Same as @array = (11, 12, 42).
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
| [reply] [d/l] [select] |
|
Re: How do i sort an numeric array without using sort function. Is ther any way to sort it just with loop in perl??
by Laurent_R (Canon) on Nov 02, 2017 at 15:05 UTC
|
You might implement a quick sort algorithm (coded in functional style):
sub qsort {
return @_ if @_ < 2;
my $pivot = pop;
qsort(grep $_ < $pivot, @_), $pivot, qsort(grep $_ >= $pivot, @_);
}
Check quick sort on Wikipedia to understand how it works. | [reply] [d/l] |
|
| [reply] |
Re: How do i sort an numeric array without using sort function. Is ther any way to sort it just with loop in perl??
by choroba (Cardinal) on Nov 02, 2017 at 09:34 UTC
|
One of the simplest algorithms is the Gnome sort:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
my @arr = (5, 3, 2, 1, 4);
my $i = 0;
while ($i < $#arr) {
# Uncomment the following line to see the progress:
# say STDERR "# @arr[ 0 .. $i ] * @arr[ $i + 1 .. $#arr ]";
if ($arr[$i] > $arr[$i+1]) {
@arr[$i, $i+1] = @arr[$i+1, $i];
--$i if $i;
} else {
++$i;
}
}
say "@arr";
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
| [reply] [d/l] [select] |
|
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @unsorted = map {int (1000 * rand ())} (1..100);
my @sorted = sort_try (@unsorted);
print Dumper \ @sorted;
sub sort_try {
my $i = 0;
while ($i < $#unsorted) {
if ($unsorted[$i] > $unsorted[$i+1]) {
@unsorted[$i, $i+1] = @unsorted[$i+1, $i];
--$i if $i;
} else {
++$i;
}
} return @unsorted;
}
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
|
Re: How do i sort an numeric array without using sort function. Is ther any way to sort it just with loop in perl??
by thanos1983 (Parson) on Nov 02, 2017 at 09:44 UTC
|
Hello Tabish,
Welcome to the Monastery. This questions has been asked before on the forum Sorting Arrays Without using SORT function. You can find more information and suggestions there. :D
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
| [reply] [d/l] [select] |
|
| [reply] |
Re: How do i sort an numeric array without using sort function. Is ther any way to sort it just with loop in perl??
by karlgoethebier (Abbot) on Nov 03, 2017 at 06:38 UTC
|
| [reply] [d/l] |
|
| [reply] |