CS210 · Annotated SlidesSorting — Merge Sort
Sorting · Annotated slides

Merge Sort

Mergesort: merging, the recursive top-down sort and bottom-up version, its N log N analysis and extra memory, comparators, and stability.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up
Slide 1: A l g o r i t h m s
Exam weight Low
    Slide 2: Two classic sorting algorithms: mergesort and quicksort
    Exam weight Low
    1. Mergesort: sort each half recursively, then merge.
    Slide 3: Slide 3
    Exam weight Low
      Slide 4: w.SI
      Exam weight Low
        Slide 5: Abstract in-place merge demo
        Exam weight Low
          Slide 6: Abstract in-place merge demo
          Exam weight Low
            Slide 7: a 5 7
            Exam weight Low
              Slide 8: only
              Exam weight Low
                Slide 9: Mergesort: trace
                Exam weight Low
                  Slide 10: Mergesort: animation
                  Exam weight Low
                    Slide 11: Mergesort: animation
                    Exam weight Low
                      Slide 12: Mergesort: empirical analysis
                      Exam weight Low
                        Slide 13: Mergesort: number of compares
                        Exam weight High
                        1. Mergesort uses at most N lg N compares (and ~6 N lg N array accesses).
                        🎯 How this can come up · 1 question
                        MCQ

                        Mergesort’s running time in the worst case is:

                        Slide 14: Divide-and-conquer recurrence: proof by picture
                        Exam weight Low
                          Slide 15: Divide-and-conquer recurrence: proof by induction
                          Exam weight Low
                            Slide 16: Mergesort: number of array accesses
                            Exam weight Low
                              Slide 17: Mergesort analysis: memory
                              Exam weight High
                              1. Mergesort needs extra memory proportional to N (the aux array): not in place.
                              🎯 How this can come up · 1 question
                              True / False

                              Standard mergesort sorts in place with constant extra memory.

                              Slide 18: Java 6 system sort
                              Exam weight Low
                                Slide 19: Slide 19
                                Exam weight Low
                                  ★ Past exam
                                  Slide 20: Bottom-up mergesort
                                  Exam weight High
                                  1. Bottom-up mergesort: merge subarrays of size 1, 2, 4, 8… without recursion.
                                  🎯 How this can come up · 1 question
                                  Written

                                  Merge sort [RUH, AMM, DMM, JED, KWI, DXB, SFO]. (Quiz 2)

                                  Slide 21: Bottom-up mergesort: Java implementation
                                  Exam weight Low

                                    Mergesort: visualizations

                                    _Ii

                                    arrays

                                    " = 12)

                                    " = 12)

                                    top-down mergesort (cuto

                                    bottom-up mergesort (cuto

                                    22
                                    Exam weight Low
                                      Slide 23: Sort countries by gold medals
                                      Exam weight Low
                                        Slide 24: Sort countries by total medals
                                        Exam weight Low
                                          Slide 25: Sort music library by artist
                                          Exam weight Low
                                            Slide 26: Sort music library by song name
                                            Exam weight Low
                                              Slide 27: Comparable interface: review
                                              Exam weight Low
                                                Slide 28: Comparator interface
                                                Exam weight Low
                                                  Slide 29: Comparator interface: system sort
                                                  Exam weight Low
                                                    Slide 30: Comparator interface: using with our sorting libraries
                                                    Exam weight Low

                                                      Comparator interface: implementing

                                                      To implement a comparator:

                                                      Define a (nested) class that implements the Comparator

                                                      interface.

                                                      ・ ・

                                                      Implement the compare

                                                      method.

                                                      public class Student { private final String name; private final int section; ...

                                                      public static class ByName implements Comparator<Student> { public int compare(Student v, Student w) { return v.name.compareTo(w.name); } } public static class BySection implements Comparator<Student> { public int compare(Student v, Student w) { return v.section - w.section; } } }

                                                      this trick works here

                                                      since no danger of overflow

                                                      31
                                                      Exam weight Low
                                                        Slide 32: Comparator interface: implementing
                                                        Exam weight Low
                                                          Slide 33: Slide 33
                                                          Exam weight Low
                                                            Slide 34: Stability
                                                            Exam weight High
                                                            1. Stable sort: equal keys keep their relative order. Insertion and merge sort are stable; selection sort (and quicksort) are not.
                                                            🎯 How this can come up · 1 question
                                                            MCQ

                                                            Which sort is stable?

                                                            Slide 35: Stability
                                                            Exam weight Low
                                                              Slide 36: Stability: insertion sort
                                                              Exam weight Low
                                                                Slide 37: Stability: selection sort
                                                                Exam weight Low
                                                                  Slide 38: Stability: mergesort
                                                                  Exam weight Low
                                                                    Slide 39: Stability: mergesort
                                                                    Exam weight Low
                                                                      Slide 40: Sorting summary
                                                                      Exam weight Low