Mergesort’s running time in the worst case is:
Merge Sort
Mergesort: merging, the recursive top-down sort and bottom-up version, its N log N analysis and extra memory, comparators, and stability.
How to read this page
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
All 40 slides
- 1 A l g o r i t h m s
- 2 Two classic sorting algorithms: mergesort and quicksort
- 3 Slide 3
- 4 w.SI
- 5 Abstract in-place merge demo
- 6 Abstract in-place merge demo
- 7 a 5 7
- 8 only
- 9 Mergesort: trace
- 10 Mergesort: animation
- 11 Mergesort: animation
- 12 Mergesort: empirical analysis
- 13 Mergesort: number of compares
- 14 Divide-and-conquer recurrence: proof by picture
- 15 Divide-and-conquer recurrence: proof by induction
- 16 Mergesort: number of array accesses
- 17 Mergesort analysis: memory
- 18 Java 6 system sort
- 19 Slide 19
- 20 Bottom-up mergesort
- 21 Bottom-up mergesort: Java implementation
- 22 Mergesort: visualizations
- 23 Sort countries by gold medals
- 24 Sort countries by total medals
- 25 Sort music library by artist
- 26 Sort music library by song name
- 27 Comparable interface: review
- 28 Comparator interface
- 29 Comparator interface: system sort
- 30 Comparator interface: using with our sorting libraries
- 31 Comparator interface: implementing
- 32 Comparator interface: implementing
- 33 Slide 33
- 34 Stability
- 35 Stability
- 36 Stability: insertion sort
- 37 Stability: selection sort
- 38 Stability: mergesort
- 39 Stability: mergesort
- 40 Sorting summary
Two classic sorting algorithms: mergesort and quicksort
Notes
Mergesort: sort each half recursively, then merge.
Mergesort: number of compares
Exam angle
Mergesort analysis: memory
Exam angle
Standard mergesort sorts in place with constant extra memory.
- It needs an auxiliary array of size N.
Bottom-up mergesort
Exam angle
merge sort tree for airport codes
Merge sort [RUH, AMM, DMM, JED, KWI, DXB, SFO]. (Quiz 2)
Compare with the model answer
Split to single items, then merge: [AMM, RUH] [DMM, JED] [DXB, KWI] [SFO] → [AMM, DMM, JED, RUH] [DXB, KWI, SFO] → [AMM, DMM, DXB, JED, KWI, RUH, SFO].
Stability
Notes
Stable sort: equal keys keep their relative order. Insertion and merge sort are stable; selection sort (and quicksort) are not.
Exam angle
Which sort is stable?