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.

screen-shot-2016-11-14-at-3-45-23-pm

I actually rewrote it in Ruby first since I’m more used in thinking in Ruby now (I guess that’s a good thing?)

screen-shot-2016-11-14-at-3-47-20-pm

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!

screen-shot-2016-11-14-at-3-46-56-pm

Advertisements
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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s