in reply to crop-sort: Maintaining a Top-10 Array

I think you're swapping unnecessarily. Consider this algorithm:
  1. start with item n where n = 10. if my insert item is less than that, exit
  2. if my insert item is bigger than item n, then set item n to my insert item, and quit
  3. reduce n by 1, copy item n to item n+1 and repeat from the previous step
See.. I don't see any swaps needed. Just a push-down.

Of course, if you find the location using a binary search, it might be faster, and then use a splice to insert it, and you're now at the other algorithm. {grin}

-- Randal L. Schwartz, Perl hacker

  • Comment on Re: crop-sort: Maintaining a Top-10 Array