Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: Algorithm Pop Quiz: Sorting

by Jasper (Chaplain)
on Mar 25, 2002 at 14:08 UTC ( [id://154092]=note: print w/replies, xml ) Need Help??


in reply to Re: Algorithm Pop Quiz: Sorting
in thread Algorithm Pop Quiz: Sorting

I knocked together an almost identical bit of code, apart from the gt bit, which really is the same:
sub sort_stack { local $depth = pop @stack; local $sort_depth = $depth; for (1..$depth) { for (1..$sort_depth) { local $top = pop @stack; local $next = pop @stack; if ($top gt $next) { $top ^= $next; $next ^= $top; $top ^= $next; } push @stack, $next, $top; rotate_up($sort_depth); } rotate_up($sort_depth); --$sort_depth; } push @stack, $depth; }
Although I hadda go and use an xor swap, to make it look quite cool..

Replies are listed 'Best First'.
XOR swap bad...
by RMGir (Prior) on Mar 25, 2002 at 14:22 UTC
    Sure, it looks cool. It's just wrong :)

    The problem with xor on strings is it will extend short ones. There are now 0 bytes there that weren't there before, and length changes.

    $ perl -e'$a="a"; $b="bbb"; $a^=$b; $b^=$a; $a^=$b; print "/$a/,/$b/\n +"; print length($a),"\n"; print length($b),"\n"' /bbb/,/a/ 3 3

    --
    Mike
      Mike, thanks, I think. How odd. I had no idea this happened. That'll teach me. Still, I've never had a computer science lesson in my life, so how am I expected to know these things!?! :) Cheers, Jasper
        Hehe, to tell you the truth, I DO have a CS background, and I never would have known this happened in perl if I hadn't run into the string extension problem using the preserve_case routine from perlfaq6.
        --
        Mike
Re: Re: Re: Algorithm Pop Quiz: Sorting
by clintp (Curate) on Mar 25, 2002 at 15:18 UTC
    BZZZT

    I'm going to disallow this one as it's relying on a feature that's language dependent for its implementation (the XOR swap for stings). The sprit is there, but you're starting to wander. This kind of behavior needs to be discouraged early! Use the extra register for the swap. :)

    These are all so good though. The excitement is terrible. Just terrible!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found