A key step in many sorting algorithms (including selection sort) is swapping the location of two items in an array. Here’s a swap
function that looks like it might work, but doesn’t:
-the code prints out [9, 9, 4]
when it should print out [9, 7, 4]
.
Fix the swap
function.
I actually rewrote it in Ruby first since I’m more used in thinking in Ruby now (I guess that’s a good thing?)
Here’s my work in javaScript. Took me a while to figure out how to define a variable inside the function.. Nevertheless, all test cases passed!
Advertisements