http://qs321.pair.com?node_id=232886


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.

update


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".

    CountZero

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