One of my guides at Dev Bootcamp has been feeding me a steady diet of awesome resources, and I've been eating them up like a sponge. (That metaphor got away from me.) One of the resources he pointed me towards is a Sorting Algorithm Animations site, and I've been mesmerized by the different algorithms for sorting that it showcases...in large part because I don't understand how they work.
In college, one of my favorite English professors told me that the best papers are actually an attempt to answer a question where the author doesn't already know the answer before they start researching. I took this advice to heart, and it's helped me stay playful and curious as I learn code. Today's playful and curious experience is with the bubble sort.
This was probably my
third or fourth time writing a bubble sort from scratch. The first time I wrote one, I didn't know that Ruby allowed
you to switch the indexes of two objects in an array by using
array[i], array[j] = array[j], array[i],
and I struggled with trying to find the appropriate methods to put those items into the appropriate positions.
This second was a different bubble sort that
I had written to an RSpec. It doesn't accept an array, but instead acts upon
self, and can take a
Proc as shown in the RSpec snippet below.
bubble_sort! was extremely difficult because I couldn't figure out where the block
came into play. Was I supposed to
yield to it before the program actually started sorting
or somewhere else? What exactly was the block passing and how? All these sorts of things always feel very fuzzy to me,
and in many ways my main struggle with Ruby is to simply get it to be less magical to me. That said, having to pass a block
bubble_sort! was extremely helpful in understanding just exactly what is being passed, and when to
call on it.