c++ - How to k-shuffle an array with STL? -


i testing algorithm sorts k-sorted array (every element @ k-positions away correct sorted position).

i having hard time generating test data -- can't randomly swap elements k-positions because may end swapping element twice. track elements swapped need o(n) space. use random-heap of size k + 1, sounds silly.

is there built stl can me this? seems common problem, brief research turned algorithms total shuffles (i think stl implements fisher-yates).

it feels odd problem since preparing random test data not need ultra efficient, data may whatever. can have test values correct positions of elements or pairs give range of correct positions. example array of pairs:

  1. 1,1
  2. 2,4
  3. 2,4
  4. 2,4
  5. 5,6
  6. 5,6
  7. 7,7
    ...

store state of random generator somewhere.

choose 2 random elements position not more k positions away of original position (or range) of other , swap. repeat n times , test data ready.

if need same sequence later restore random generator state , repeat algorithm.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -