Algorithm to print out a shuffled list, in-place and with O(1) memory

后端 未结 10 1412
野趣味
野趣味 2020-12-09 11:51

After reading this question I started to wonder: is it possible to have a shuffling algorithm which does not modify or copy the original list?

To make it clear:

10条回答
  •  猫巷女王i
    2020-12-09 12:34

    It's not possible to do this with a truly random number generator since you either have to:

    • remember which numbers have already been chosen and skip them (which requires an O(n) list of booleans and progressively worsening run-times as you skip more and more numbers); or
    • reduce the pool after each selection (which requires either modifications to the original list or a separate O(n) list to modify).

    Neither of those are possibilities in your question so I'm going to have to say "no, you can't do it".

    What I would tend to go for in this case is a bit mask of used values but not with skipping since, as mentioned, the run-times get worse as the used values accumulate.

    A bit mask will be substantially better than the original list of 39Gb (10 million bits is only about 1.2M), many order of magnitude less as you requested even though it's still O(n).

    In order to get around the run-time problem, only generate one random number each time and, if the relevant "used" bit is already set, scan forward through the bit mask until you find one that's not set.

    That means you won't be hanging around, desperate for the random number generator to give you a number that hasn't been used yet. The run times will only ever get as bad as the time taken to scan 1.2M of data.

    Of course this means that the specific number chosen at any time is skewed based on the numbers that have already been chosen but, since those numbers were random anyway, the skewing is random (and if the numbers weren't truly random to begin with, then the skewing won't matter).

    And you could even alternate the search direction (scanning up or down) if you want a bit more variety.

    Bottom line: I don't believe what you're asking for is doable but keep in mind I've been wrong before as my wife will attest to, quickly and frequently :-) But, as with all things, there's usually ways to get around such issues.

提交回复
热议问题