5.3-6 PERMUTE-BY-SORTING, if two or more priorities are identical

Problem 5.3-6

Explain how to implement the algorithm PERMUTE-BY-SORTING to handle the

case in which two or more priorities are identical. That is, your algorithm should

produce a uniform random permutation, even if two or more priorities are identical


Solution 1

1. Redo the procedure until no conlision occurs.


Solution 2:

1. Get all items with the same priority

2. Assign priority again randomly.

3. sort.

4. If there are still two or more priorities are the same, go to step 1.

4. Put back to the origin position.

Analyse: 

Randomly permute the parts with colision only, to save the time for sort.

你可能感兴趣的:(Intro,to,Algorithms,3rd.ed)