in reply to A more efficient sort or heap algorithm...

Why are you using a hand-coded bubble sort (which is a very poor algorithm at best) instead of Perl's built-in sort?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re: A more efficient sort or heap algorithm...

Replies are listed 'Best First'.
Re^2: A more efficient sort or heap algorithm...
by bioinformatics (Friar) on Apr 28, 2009 at 22:35 UTC
    I'm still learning about efficiency (obviously), but the benefit of using the hand coded sort was to allow me to have two arrays sorted the same way (but based on the first, if that makes sense), with the second array containing a hash key leading to the data for the corresponding region. I wasn't sure how to implement that using the built in sort function. It allows me to keep track of and print out the information for both regions.
    Bioinformatics
      I wasn't sure how to implement that using the built in sort function. It allows me to keep track of and print out the information for both regions.

      Just try to sort the indices and then map both arrays by them:

      my @ind_srtd = sort { $array->[$a] cmp $array->[$b] } (0..$#$array); my @array_srtd = map { $array->[$_] } @ind_srtd; my @array_which_srtd = map { $array_which->[$_] } @ind_srtd;

      I didn't tested it thoroughly, but should look something like this.