Swap Function

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!


Swap Function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s