Selection Sort (120)

Back to Problem Solutions forum

astjoemd     2016-02-17 10:42:49

According to the example:

input data:
6
31 41 59 26 53 58
answer:
2 2 2 1 0

But my code gives a different answer, 2 4 3 1 0. I've tried to think it out and it seems that my answer is actually right:

31 41 59 26 53 58 -> max at 2
31 41 26 53 58 -> max at 4
31 41 26 53 -> max at 3
31 41 26 -> max at 1
31 26 -> max at 0

But according to the code checker, this answer is wrong, and I can't figure out why. May I get some explanation on this? (it actually looks like it goes the max-min-max-min way)

Photon     2016-02-17 13:17:10
User avatar

31 41 59 26 53 58 -> max at 2
When you find max element you switch it with the last one. So array should be:
31 41 58 26 53
after first pass.

astjoemd     2016-02-17 15:40:40

Oh, swapping. I feel kind of blind now. Thanks for making it clear for me!

Please login and solve 5 problems to be able to post at forum