Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Perl sorts

Name: Anonymous 2011-04-28 15:06

I'm trying to do a bubble sort, selection sort, merge sort(not implemented yet) in perl but this randomly crashes no clue why any ideas...?


#!/bin/perl


&main();

sub main
{
    my @array = (1,7,4,9,4,7,2,3,0,8);

    print "Selection Sort\n\n";

    &selectionSort(\@array,);
    print "@array\n\n";

    print "Bubble sorting\n\n"; #crashes here sort of clears the terminal screen and prints 8,9

    my @secondarray =(7,8,9,3.2.10.12);
    bubble_sort(@secondarray);

    print "@secondarray\n\n";

    sleep(10);
} # main

sub selectionSort
{
    my $aref = shift(@_);

    for (my $x=0; $x<@$aref-1; $x++)
    {
        my $smallestIndex = $x;
        for (my $y=$x+1; $y<@$aref; $y++)
        {
         if ( $aref->[$y] < $aref->[$smallestIndex] )
         {
             $smallestIndex = $y;
         }
        } # for $y
        &swap( \$aref->[$x], \$aref->[$smallestIndex] );
    } # for $x
} # selectionSort

sub swap

{
    my ($x, $y) = @_;
    my $tmp = $$x;
    $$x = $$y;
    $$y = $tmp;
} # swap


sub bubble_sort {
    for my $i (0 .. $#_){
        for my $j ($i + 1 .. $#_){
            $_[$j] < $_[$i] and @_[$i, $j] = @_[$j, $i];
        }
    }
}

Name: Anonymous 2011-04-28 15:07

also i know the sorting code is right since they work when used apart from one another

Name: Anonymous 2011-04-28 15:28

Asking /prauge/ to "help" you with your homework are you?

1st semester programming shit: bubble sorts and etc.

Protip #0: The rest of us Perl's built sort function.
Protip #1: We are not here to do your homework for you, expect answers in every language *but* Perl. Write your own damn code, kid

Name: Anonymous 2011-04-28 15:32

>>3
ummm this is for myself I'm also doing linear search binary search hash sort some shell programming

if this was for a class I'd ask the damn professor

Name: Anonymous 2011-04-28 15:37

You obviously don't have enough $ and @. You have to put them EveryWHERE

Name: Anonymous 2011-04-28 15:45

.'s != ,

Name: Anonymous 2011-04-28 15:47

>>5
Exactly. The perl parser runs on $ and @. If it doesn't find enough of them on the way it starves, which is exactly what happens at line 15 of your program. You can try to jump start it with a % or just place more goddamn $ and @.

Name: Anonymous 2011-04-28 16:58

>>4
Lies

Name: Anonymous 2011-04-28 16:59

>>8
objection truth

Name: HAXUS THE JUDGE 2011-04-28 17:23

>>9
Objection over rulled.  Counsel >>8, please proceed. Any more out of you >>9 and I will hold you in Contempt.

Name: Anonymous 2011-04-28 17:29


VICTORY TO THE FARTISTS!

Name: Anonymous 2011-04-28 17:34


VICTORY TO THE FARTISTS!

Name: Anonymous 2011-04-28 19:21


VICTORY TO THE FARTISTS!

Name: Anonymous 2011-04-28 19:21


VICTORY TO THE FARTISTS!

Name: Anonymous 2011-04-28 19:33

But whom will save Xarn?

Don't change these.
Name: Email:
Entire Thread Thread List