开发者

Which sorting algorithm is used here?

In a programming event they asked this question.

Question: 1 2 3 4 5 6 7 8 9 10 J Q K.

Write and algorithm to sort the cards in reverse order 开发者_如何转开发in 7 steps.

K Q J 10 9 8 7 6 5 4 3 2 1.

I couldn't trace out the sorting alogrithm. Which algoritm is used here?


I think it's something like quicksort, but with block moves:

1 2 3 4 5 6 7 8 9 10 J Q K

8 9 10 J Q K 7 1 2 3 4 5 6
J Q K 8 9 10 7 1 2 3 4 5 6 
J Q K 8 9 10 7 4 5 6 1 2 3 
K Q J 8 9 10 7 4 5 6 1 2 3 
K Q J 10 9 8 7 4 5 6 1 2 3 
K Q J 10 9 8 7 6 5 4 1 2 3 
K Q J 10 9 8 7 6 5 4 3 2 1

Update: actually it might be even simplier: just swap 1 and K and 2 and Q, 3 and J etc. Just seven steps :)


If a step is a swap then Selection sort will order the list in 6 steps.

K Q J 10 9 8 7 6 5 4 3 2 1

1 Q J 10 9 8 7 6 5 4 3 2 K

1 2 J 10 9 8 7 6 5 4 3 Q K

1 2 3 10 9 8 7 6 5 4 J Q K

1 2 3 4 9 8 7 6 5 10 J Q K

1 2 3 4 5 8 7 6 9 10 J Q K

1 2 3 4 5 6 7 8 9 10 J Q K

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜