Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

merge sort

  • 01-05-2006 06:41PM
    #1
    Closed Accounts Posts: 839 ✭✭✭


    can someone explain how a merge sort works, has anyone any examples?


Comments

  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    There's a very good explanation here with Java source.


  • Closed Accounts Posts: 839 ✭✭✭zap


    ok i get say that you have

    an array 1 5 6 7 8 9

    so that gets split into

    1 5 6 and 7 8 9


    where do i go after that?

    1 5 6 splits into

    1 5 6

    then


    1


    is that correct


  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    Basically you code it as a recursive function

    sort(array) {
    if (arraylength = 1) return

    sort(1st half)
    sort(2nd half)
    merge(1st half, 2nd half)
    }

    Each 1/2 gets fired back into the 'sort' algorithm, you keep splitting in half and merging until each half only has one element in it.

    You don;t actualy write a 'sort' the only thing that matters is the merge, which basically has 2 ordered lists and has to make one ordered list from both parts. There are a number of ways of writing the merge more efficiently.


  • Registered Users, Registered Users 2 Posts: 1,821 ✭✭✭Skud


    http://math.hws.edu/TMCM/java/xSortLab/

    This should help you out. Explains the visual worikings of some common sort algorithms


Advertisement