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

来源:互联网 发布:自制手机壁纸的软件 编辑:程序博客网 时间:2024/06/05 06:21

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.

原创粉丝点击