Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
Trust the World's Fastest VPN with Your Internet Security & Freedom - A Lifetime Subscription of PureVPN at 88% off. Also, Slashdot's Facebook page has a chat bot now. Message it for stories and more. ×
Perl

Journal Journal: Merge Sort

A little snippet of Perl, and the first Perl I have done in a long while. Still waiting for Perl 6...

sub merge {
    my ($left, $right) = @_;
    my @merged;
 
    while ((scalar(@$left) > 0) && (scalar(@$right))) {
 
        if ($left->[0] <= $right->[0]) {
            push @merged, shift(@$left);
        } else {
            push @merged, shift(@$right);
        }
    }
 
    if (scalar(@$left) > 0) {
        push @merged, shift(@$left);
    }
 
    if (scalar(@$right) > 0) {
        push @merged, shift(@$right);
    }
 
    return @merged;
}
 
sub mergesort {
    my @list = @_;
    my (@left,@right);
 
    if (scalar(@list) <= 1) {
        return @list;
    } else {
 
        my $middlin = scalar(@list) / 2;
 
        foreach (0..($middlin - 1)) {
            push @left, $list[$_];
        }
 
        foreach ($middlin..(scalar(@list) - 1)) {
            push @right, $list[$_];
        }
 
        @left = mergesort(@left);
        @right = mergesort(@right);
 
        return &merge(\@left,\@right);
    }
}

Education

Journal Journal: Rules for Writers

Avoid run-on sentences they are hard to read. Don't use no double negatives. Use the semicolon properly, always use it where it is appropriate; and never where it isn't. Reserve the apostrophe for it's proper use and omit it when its not needed. No sentence fragments. Avoid commas, that are unnecessary. Eschew dialect, irregardless. And don't start a sentence with a conjunction. Hyphenate between sy-llables and avoid un-necessary hyphens. Write all adverbial forms correct. Don't use contractions in formal writing. Writing carefully, dangling participles must be avoided. It is incumbent on us to avoid archaisms. Steer clear of incorrect forms of verbs that have snuck in the language. Never, ever use repetitive redundancies. If I've told you once, I've told you a thousand times, resist hyperbole. Also, avoid awkward or affected alliteration. Don't string too many prepositional phrases together unless you are walking through the valley of the shadow of death. "Avoid overuse of 'quotation "marks."'"

Slashdot Top Deals

Artificial intelligence has the same relation to intelligence as artificial flowers have to flowers. -- David Parnas

Working...