Why is mege sort O(n log n)?

Discussion in 'C' started by Whilliam, Oct 5, 2010.

  1. Whilliam

    Whilliam New Member

    Joined:
    Oct 5, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Example, n is 8.
    Merge sort (for link list) is constantly divide the list into two sublist until every node->next will point to null. Then, sort them by merging them.

    When I tried to count it myself, in the divide part
    first divide the 8 nodes into two. In link list, you will need to traverse. Since traversing in merge sort is n/2, I will have 4 traversals.
    After dividing the 8 nodes into two, I will divide the left sublist (which has 4) and the right sublist (which also has 4) into two. The left sublist will have 2 traversal, as well as the right sublist.
    Sum it all up, it will be O(8).
    Then, divide the 4 groups of 2 into half, we will add 4 to O(8).
    So technically, it's O(12) in the divide part.
    Then in the merge part, it's actually the same.
    So it should be O((n + 4)*(n + 4)).
    Can anyone explain to me why merge sort is O(n log n)?
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice