Mergesort
About this app
Mergesort as in the lecture program: a tree of sublists grows step by step, first the left half completely, then the right, then merge; the edges are numbered as on the exercise sheets. Next to it the merge in large: two sorted lists, their front elements, the target list with free slots and travel lines, one sentence per step. Python code with the running line, counters, predictions, a level view for counting. Example 11, 9, 2, 6, 4, 3, 1, 7, 8 with 17 comparisons; always O(n log n), Quicksort alongside.
With explain mode: an AI tutor walks you through the app, points out what matters in the scene and answers your questions.
Split and merge
Merge sort splits a list into two halves, n // 2 elements on the left, the rest on the right. It sorts the left half completely first, then the right one, both with the same method, and then merges the two sorted halves. A list with one element is sorted. While merging, only the two front elements are compared; the smaller one goes into the target list, on ties the left one. If one list is empty, the rest is added without a comparison. That is why merge sort needs O(n log n) comparisons in every case.
What the app shows
On the left a tree of sublists grows as on the exercise sheets: split downwards, merged below, and the edges are numbered in the order of the program. On the right the running merge, large: the list with indices, the two sorted lists, their front elements, and below them the target list with free places and lines showing where each element came from. Each step has a sentence, such as “9 is smaller than 11, goes into the target list.” Alongside runs the Python code with the current line and the call stack.
Try it yourself
Step through the example 11, 9, 2, 6, 4, 3, 1, 7, 8: 17 comparisons, and the right half only starts once the left one is fully sorted. Predictions ask before every comparison which element goes into the target list next. The Levels view rearranges the sublists for counting: at most n comparisons per level. A sorted list of nine numbers costs 13 comparisons, Quicksort would need 36. Your own numbers, up to twelve, are possible.
Frequently asked questions
- How does merging work in merge sort?
- Both sublists are sorted, so the smallest remaining element is at the front of one of them. The two front elements are compared, the smaller one is added at the end of the target list, and in its list the next one moves to the front. If one list is empty, the rest of the other is added without a comparison. Two lists with n elements together need at most n − 1 comparisons.
- Why does merge sort always need O(n log n) comparisons?
- Each split halves; after ⌈log₂ n⌉ splits every element stands alone. On each merge level every comparison puts one element into a target list, so at most n comparisons per level. That holds for every input, sorted or reversed lists included. Unlike Quicksort with the last element as pivot, merge sort has no unfavourable case with n(n−1)/2 comparisons.
- Is merge sort stable?
- Yes. Merging tests a[i1] <= a[i2]: if both are equal, the element of the left list comes first, so equal values keep their original order. Apart from that, merge sort needs a helper list with n places and copies it back.
- What does the app Merge Sort show?
- The run of the lecture's Python program as a growing tree with numbered edges, the running merge with front elements and target list, a sentence for every step, the Python code with the running line, counters for comparisons and writes, prediction questions, a levels view for counting and your own lists.
Subject: Computer science
More from Computer science
- Ackermann function
- Call Tree
- Elevator simulator
- Binary workshop
- Binary versus linear search
- The Collatz problem
- Inside a CPU
- Swap the inside
A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps