In a programming event they asked this question.
Question: 1 2 3 4 5 6 7 8 9 10 JQ K.
Write and algorithm to sort the cards in reverse order in 7 steps .
KQJ 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.
KQJ 10 9 8 7 6 5 4 3 2 1
1 QJ 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 QK
1 2 3 4 9 8 7 6 5 10 JQK
1 2 3 4 5 8 7 6 9 10 JQK
1 2 3 4 5 6 7 8 9 10 JQK
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.