MERGESORT STUDY GUIDE


Mergesort. Merge left, merge right, merge.

Merge. Understand how to carry out the merge operation. How many compares does it use when comparing two arrays of size N in the best case? In the worst case?

Mergesort order of growth. Understand how to show that the order of growth of the number of compares is N lg N. Understand why the entire algorithm is also order N lg N.

Mergesort compare bounding. Know why the best case is ~ 1/2 N lg N and the worst case is ~ N lg N compares.

Mergesort properties. Mergesort is stable (why?). Mergesort uses N extra memory (why?). Does mergesort do particularly well on already sorted arrays? Partially ordered arrays?

Recommended Problems

C level

  1. Give a worst case and a best case input for mergesort.

B level

  1. Textbook 2.2.22
  2. Textbook 2.2.8
  3. We would like to sort playing cards from a deck. Associated with each card is a denomination (1 to 13) and a suit
    (CLUBS < DIAMONDS < HEARTS < SPADES). A card c1 is considered less than a card c2 if either of the following is true:
    - the suit of c1 is less than the suit of c2, or
    - c1 and c2 are of the same suit, but the denomination of c1 is less than the denomination of c2.

    Suppose that the variable cards is an array of cards. We could sort it, using your compareTo function, with a call to MergeX.sort(cards). Which of the following code fragments would produce an equivalent final result? Circle all equivalent code fragments.

        Option 1:
        MergeX.sort(cards, Card.SUIT_ORDER);
        MergeX.sort(cards, Card.DENOM_ORDER);
        Option 2:
        MergeX.sort(cards, Card.DENOM_ORDER);
        MergeX.sort(cards, Card.SUIT_ORDER);
        Option 3:
        MergeX.sort(cards);
        MergeX.sort(cards, Card.SUIT_ORDER);
        Option 4:
        MergeX.sort(cards, Card.DENOM_ORDER);
        MergeX.sort(cards);
        Option 5:
        Quick.sort(cards, Card.SUIT_ORDER);
        Quick.sort(cards, Card.DENOM_ORDER);
        Option 6:
        Quick.sort(cards, Card.DENOM_ORDER);
        Quick.sort(cards, Card.SUIT_ORDER);
        Option 7:
        MergeX.sort(cards);
        Quick.sort(cards, Card.SUIT_ORDER);
        Option 8:
        Quick.sort(cards, Card.DENOM_ORDER);
        MergeX.sort(cards);    
    Answers

A level

  1. Using the decision tree idea, rederive the proof that all compare-based sorting algorithms must use at least ~ N lg N compares in the worst case. You may assume Stirling's formula.