RSPEC 2 : BubbleSort

In the recent lessons leading up to this, I’ve had a few questions that haven’t been answered by previous bubble sort questions:

  1. The self[i], self[j] = self[j], self[i] line is switching i to j and j to i in that order correct?
  2. I know it’s best practice to comprehend the proc way of creating bubble sort, but does it exclusively cover all edge-cases? Are there going to be future examples upcoming in the course where I need to do it without procs?
  3. For this tiny line here:
    def bubble_sort(&prc)

Is this deferring to all calls made to our bubble_sorts? What is it’s purpose as a concluding method?

  1. So if I’m getting the line below correctly, either prc exists first off (which short-circuits the latter…) or it does not exist and we create a… Am I correct?
    prc ||= { |x, y| x <=> y }
  1. The order does not really matter, but yes, in parallel assignment, i switches to j and j to i.
  2. You should try writing bubble sort without a proc that covers all edge-cases. It is not that different from the proc version and you will also understand more about what the proc is doing.
  3. The method there is different bubble_sort method that does not modify the original array. The self.dup creates a copy of the array and then modifies the copy using bubble_sort!, which does modify the array.
  4. You are correct. the ||= is a short-circuit that will only call if prc does not exist.

Michael Ruan

1 Like

Thanks! Will try to do so when I’ve caught up.

Ah I see, so in reality we aren’t messing with the original array due to the last definition which sets up our mapped array to then commence the algorithm on. Do I have this line of logic correct?

Kinda. I am not sure what you mean by mapped array. The array is duplicated using dup and the copy is modified using the algorithm so the original array is not modified.