in reply to Re^2: In-place sort with order assignment
in thread In-place sort with order assignment

BrowserUk,
You didn't comment on my multi-pass each approach. It is an ugly solution but there is no reason I can think of that it wouldn't work.

Cheers - L~R

  • Comment on Re^3: In-place sort with order assignment

Replies are listed 'Best First'.
Re^4: In-place sort with order assignment
by BrowserUk (Patriarch) on Sep 19, 2010 at 15:04 UTC
    You didn't comment on my multi-pass each approach.

    Um...mostly, because I didn't understand it. Or at least, I'm still pondering how it might be implemented?

      BrowserUk,
      Hrm. It sounds really close if not identical to the one described by Corion. If I didn't have kids running around screaming "Daddy Daddy", I would code up a solution but a better description will have to do.

      I am confident that, in addition to my hash, I can have two additional arrays with 1000 items each. On the first pass through the hash using each, you check to see if the current key in the hash is lt than the first element in the array. If yes, you unshift, if not you check the next item until you find the proper location and use splice. If you reach the end of the array and it is less than 1000, you push. At the end, you check if you have 1001 items and you pop. At the end of the first pass, you now know the first 1000 items. You begin your second pass. This time, when you encounter an item in your first array, you assign its appropriate value while simultaneously populating the 2nd array (using the last element of the first).

      Now of course this is silly - there are much better data structures than an array but I hope you get the idea.

      Cheers - L~R

        I guess I'm being thick here but, at the end of your second pass, you have 2000 items in 2 arrays, and the original number in the hash. Then what?

        And, does each "pass" mean a pass over 1000 (or 2000) elements of the array(s)? Or a full (millions) pass over the keys of the hash?


        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.