Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Tedious array sort revisited

by boo_radley (Parson)
on Feb 05, 2003 at 18:39 UTC ( #232886=note: print w/replies, xml ) Need Help??

in reply to Tedious array sort revisited

It sounds like a bubble sort. If you really can't use perl's sort for some reason ("capricious instructor mandates", getting a feel for perl's array operators, insanity, etc.), then you should at least know that it's quite easy to swap two values in an array with each other :
($array [$element_one], $array [$element_two]) = ($array [$element_two], $array [$element_one])

you should also consult perlop to see how <=> and cmp work, which can considerably reduce the amount of code needed to write your own sorting mechanism.


I really think this would have a different outcome if we could see what the actual assignment is. Something's not being communicated correctly. I'm not advocating that the assignment be posted, of course, but this isn't going well for anyone.

Replies are listed 'Best First'.
Re: Re: Tedious array sort revisited
by CountZero (Bishop) on Feb 05, 2003 at 20:17 UTC

    It is certainly not a "classic" bubble-sort but a curious mix of elements of an exchange sort (namely the swapping of the current record with the smallest remaining record) and an inverted type of insertion sort (namely finding the next smallest record and putting it directly in its place)

    None of these sorts are known to be fast or efficient and by combining them you probably get the worst elements of both.

    If this is homework, it is bad homework. I would never suggest a student to implement such a bad algorithm.

    To know more about sorting in general, one could do worse than read Don Knuth's "The Art of Computer Programming - Vol. 3 - Sorting and Searching".


    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2023-03-24 03:14 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.