Hey there! I'm glad you found it helpful. Thanks for that 😄.
The code seems to work just fine with the test list provided though. Would you mind explaining why the = is necessary? Or maybe share the list you tested the code with so I can also try it.
Ps: The = should be on the other side of the < in your example though, like so: if (list[i] > list[j-1] && list[i] <= list[j]) 🙂
if (list[i] > list[j-1] && list[i] <= list[j])
Hey Kinyanjui, thanks again for the post! Super helpful!
Just hoping on to this, I noticed an issue with the same line of code where if a list has two elements with the same value it won't sort it properly. i.e. [4, 2, 3, 2, 1, 5] gives [ 1, 2, 3, 4, 2, 5 ] as a result.
[4, 2, 3, 2, 1, 5]
[ 1, 2, 3, 4, 2, 5 ]
Adding an = sign to the first argument makes all the difference! if (list[i] >= list[j-1] && list[i] < list[j]). It now returns [ 1, 2, 2, 3, 4, 5 ] :)
if (list[i] >= list[j-1] && list[i] < list[j])
[ 1, 2, 2, 3, 4, 5 ]
Thanks Lynn. I've edited the code to include your fix.
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.