cs0401
cs0401 copied to clipboard
Selection Sort in Lab9 doesn't work
The given selection sort function in the lab9 helper file doesn't seem to work. It sorts everything except for the last element [or 2, depending on how you look at it]. The problem is in the bounds of the for loops.
Hi Zach - I do not see this problem. I tried it on all of the arrays included in a1 and it seems to sort them all fine:
Selection sort: [ 3, 4, 5, 6, 8, 9 ] Selection sort: [ 0, 1, 2, 2, 8, 9, 10, 13, 14, 17, 18, 19, 201, 220, 235 ] Selection sort: [ 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200 ] Selection sort: [ 1, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40 ] Selection sort: [ -1000, -911, -909, -900, -87, -80, -44, -32, -9, 1, 2, 3, 4, 5 , 6, 9, 11, 12, 13, 18, 20 ]
Could you give me an example of an input that causes this problem so I can reproduce?
Ohhh, I see what's happening. I was calling the selection sort method on the array. In the selection sort method there is a printArray call to print out the array. This call is before the for loops execute so it will not show the last modification to the array, because it occurs after the printArray call. I was relying only on that printout and not printing the array again after completing the sort, so I never saw that the last swap was made, my mistake.
Yes, that was just a debug print statement so people could see how the sort was working. You're right, though, it's kind of confusing. I'll remove it for next term (leaving this issue open until I get around to it). Thanks for noticing!