Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selection Sort in Lab9 doesn't work #129

Open
zta6 opened this issue Nov 5, 2015 · 3 comments
Open

Selection Sort in Lab9 doesn't work #129

zta6 opened this issue Nov 5, 2015 · 3 comments

Comments

@zta6
Copy link

zta6 commented Nov 5, 2015

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.

@laboon
Copy link
Owner

laboon commented Nov 5, 2015

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?

@zta6
Copy link
Author

zta6 commented Nov 5, 2015

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.

@laboon
Copy link
Owner

laboon commented Nov 5, 2015

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants