Skip to content
Advertisement

Why isn’t my selection sort program working?

I am trying to create my own program to do selection sort on an array of integers. I have come up with the following program, which works on some arrays, but not on others, such as this one. I have been trying to trace the problem, and I think it might have to do with where I am placing the min = num [x]; line. However, I am not sure where I should move it in order to fix the problem. Does anyone have any suggestions? Thank you.

p.s. I have provided some of my test cases and their results at the bottom.

JavaScript
Output Test Cases:
JavaScript

Advertisement

Answer

there is one big problem and multiple small ones, see comments:

JavaScript

you can simplify code a little bit:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement