Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Recursion Ruby example

I've seen this example whilst working through a non Treehouse exercise. Please could some take me through it. In #caplock I've tried to show my understanding but would really appreciate a walk through and guidance.

def mergesort(array) #NEW METHODS WHICH TAKE THE ARGUMENT ARRAY 
def merge(left_sorted, right_sorted) #A NEW METHOD, WHICH TAKES TO ARGUMENTS. 
res = [] # THE RESULT OF THIS METHOD WILL CREATE AN EMPTY STRING ASSIGNED TO VARIABLE CALLED RES. 
l = 0 #INITIAL STARTING VALUE 
r = 0 #INITIAL STARTING VALUE

loop do #LOOP 
  break if r >= right_sorted.length and l >= left_sorted.length # EXIT THE LOOP IF R >= R SORTED LENGTH. 

  if r >= right_sorted.length or (l < left_sorted.length and left_sorted[l] < right_sorted[r])
    res << left_sorted[l] #PLEASE CAN SOMEONE EXPLAIN THE STRUCTURING HERE AND THE PARENTHESIS. 
    l += 1 #INCREMENT 
  else
    res << right_sorted[r]
    r += 1 #INCREMENT 
  end
end

return res #METHOD RETURN RESULT
end

def mergesort_iter(array_sliced) #NEW METHOD WHICH TAKES THIS ARGUMENT. return array_sliced if array_sliced.length <= 1 #I DON'T GET THIS

mid = array_sliced.length/2 - 1 #OR THIS OR THE REST THE BELOW. IN PARTICULAR HOW ID MID USED BELOW? 
left_sorted = mergesort_iter(array_sliced[0..mid])
right_sorted = mergesort_iter(array_sliced[mid+1..-1])
return merge(left_sorted, right_sorted)
end

If someone could help with an idiot's guide walk though, i would be immensely grateful.

Thanks, Sara

[MOD: edited code block]

1 Answer

Houston Erekson
Houston Erekson
10,542 Points

line 11 is the same as a push method that adds a variable to the end of an array.

the entire mergesort_iter method Though it looks like there might be some code missing:

  def mergesort_iter(array_sliced) #NEW METHOD WHICH TAKES THIS ARGUMENT.
    return array_sliced if array_sliced.length <= 1 # Returns the argument/array if the array has 0 or 1 items in it
    mid = array_sliced.length/2 - 1 # Take the array length(number of items) and devide it by 2 then subtract 1
    left_sorted = mergesort_iter(array_sliced[0..mid]) #uses the mergesort_iter method with an argument of the arrays items that are between 0 and whatever mid comes up with
    right_sorted = mergesort_iter(array_sliced[mid+1..-1]) #same thing but with different items in the array
    return merge(left_sorted, right_sorted) #return everything that just happened.
  end

try testing the mergesort_iter method by calling it with an argument. play around, you wont break anything. :)

Happy Coding