Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How do i sort an numeric array without using sort function. Is ther any way to sort it just with loop in perl??

by Tabish (Acolyte)
on Nov 02, 2017 at 09:25 UTC ( [id://1202584]=perlquestion: print w/replies, xml ) Need Help??

Tabish has asked for the wisdom of the Perl Monks concerning the following question:

this is my code

use strict; use warnings; use Data::Dumper; my @unsorted = map {int (1000 * rand ())} (1..100); my @sorted = sort_try (@unsorted); sub sort_try { } print Dumper \ @sorted;
  • Comment on How do i sort an numeric array without using sort function. Is ther any way to sort it just with loop in perl??
  • Download Code

Replies are listed 'Best First'.
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";

      thanks, its nice!

      What does @s_ mean?

        @$_ 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,
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.

      Thank you, i did it

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,

      If i do any mistake its bcs im new in perl

      I didnt get any output

      #!/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; }

        You have written my @sorted = sort_try (@unsorted); but your sort_try sub doesn't return anything (and it also ignores its arguments but that's a different matter). Try reading perlsub for the basics.

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!

      Okay, Thank you!

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

      thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1202584]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-20 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found