How can I shuffle the list without accident and guarantee that some of the elements will eventually appear on one side?

Given the list of elements, is there a shuffling algorithm that ensures that in the end the selected half will be on one side and the rest on the other?

Example: {4, 3, 10, 7, 2, 9, 6, 8, 1, 5}

Given the above, I would like to have a mixing algorithm that ultimately moves marked from left to left, although the algorithm itself has no idea what is and is not “marked”.

    {4, 3, 10, 7, 2, 9, 6, 8, 1, 5}
      X X X X X

Valid results are:
    {4, 10, 9, 6, 1, 3, 7, 2, 8, 5}
    {1, 9, 10, 4, 6, 2, 8, 5, 7, 3}
  {1, 4 , 9, 10, 6, 3, 7, 5, 8, 2}, etc.

Difficulty: The algorithm should not use random numbers to mix content, it should be an iterative process. So Fisher Yates doesn't work.

+3
source share
4 answers

Would it std::next_permutation()be what you want? (Since he creates all possible permutations, he, in the end, also places the marked once to the left.)

+1
source

- . , - . , .

+5

- next_permutation , ( ) - bogo sort. , , ( - ).

Bogo , : " , . - ". . Bogo .

0

, , : * , , * , 2 ( ) ,

, - , / , :

1. Randomly select two members of the set
2. if swapping these two members would result in a marked number 
     being moved into positions 1-5 then forget about this swap and start again
3. Swap these elements
4. Check if positions 1-5 have ONLY marked elements, 
     if so you are done, otherwise start again

, Fisher-Yates, .

0

Source: https://habr.com/ru/post/1755758/


All Articles