# understanding merge sort

Discussion in 'Ruby' started by josh, Dec 17, 2011.

1. ### joshGuest

Here is the code (including the test).
http://pastebin.com/QH46wghT

it's working but i don't understand why this line:
merge_sort(a2)
works. I would think of doing this: a2 = merge_sort(a2) since i don't
see where are we assigning the return value of merge_sort into a2.

I guess there is something with recursion that I completely miss...
Thanks!

josh, Dec 17, 2011

2. ### sreservoirGuest

On 12/16/2011 11:19 PM, josh wrote:
> Here is the code (including the test).
> http://pastebin.com/QH46wghT
>
> it's working but i don't understand why this line:
> merge_sort(a2)
> works. I would think of doing this: a2 = merge_sort(a2) since i don't
> see where are we assigning the return value of merge_sort into a2.
>
> I guess there is something with recursion that I completely miss...
> Thanks!

it's an in-place sort.

sreservoir, Dec 17, 2011

3. ### David CombsGuest

In article <jcigjr\$4vm\$>,
sreservoir <> wrote:
>On 12/16/2011 11:19 PM, josh wrote:
>> Here is the code (including the test).
>> http://pastebin.com/QH46wghT
>>
>> it's working but i don't understand why this line:
>> merge_sort(a2)
>> works. I would think of doing this: a2 = merge_sort(a2) since i don't
>> see where are we assigning the return value of merge_sort into a2.
>>
>> I guess there is something with recursion that I completely miss...
>> Thanks!

>
>it's an in-place sort.

Maybe check out wikipedia, via googling merge sort.

David

David Combs, Jan 22, 2012
4. ### Robert KlemmeGuest

On 22.01.2012 03:37, David Combs wrote:
> In article<jcigjr\$4vm\$>,
> sreservoir<> wrote:
>> On 12/16/2011 11:19 PM, josh wrote:
>>> Here is the code (including the test).
>>> http://pastebin.com/QH46wghT
>>>
>>> it's working but i don't understand why this line:
>>> merge_sort(a2)
>>> works. I would think of doing this: a2 = merge_sort(a2) since i don't
>>> see where are we assigning the return value of merge_sort into a2.
>>>
>>> I guess there is something with recursion that I completely miss...
>>> Thanks!

>>
>> it's an in-place sort.

>
> Maybe check out wikipedia, via googling merge sort.

Wikipedia is a good source. But I think a good book about algorithms
and data structures (e.g. Sedgewick) should be on anyone's shelve who is
doing programming.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme, Jan 22, 2012