understanding merge sort

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

  1. josh

    josh Guest

    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
    #1
    1. Advertising

  2. josh

    sreservoir Guest

    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
    #2
    1. Advertising

  3. josh

    David Combs Guest

    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
    #3
  4. 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
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    4,486
  2. Replies:
    1
    Views:
    975
    Jonathan Bromley
    Mar 31, 2005
  3. Seth G.

    Merge Sort question

    Seth G., Aug 11, 2003, in forum: Java
    Replies:
    10
    Views:
    1,235
    Jordan Zimmerman
    Aug 15, 2003
  4. rkk
    Replies:
    9
    Views:
    812
    CBFalconer
    Sep 24, 2006
  5. Navin
    Replies:
    1
    Views:
    690
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page