Essential cookies keep your basket and sign-in working. Optional cookies help us understand visits and measure ads. Privacy details.
Stable divide-and-conquer sorting algorithm with guaranteed O(n log n) complexity.
AI-assisted explanation. It may contain errors; use a textbook or original source to check important details.
Merge sort is a way to sort a messy list by repeatedly doing a very human, very effective trick: split the problem into smaller pieces until each piece is trivially easy, then stitch the answers back together carefully. Imagine you have a shuffled stack of papers with names on them. Sorting the whole stack at once feels overwhelming. Merge sort says: cut the stack in half. Sort each half. Then merge the two sorted halves into one perfectly sorted stack by repeatedly picking the smaller “front” item from either half. Two ideas make it special: 1) Divide-and-conquer: it breaks a big task (sorting n items) into two smaller tasks (sorting about n/2 items each), over and over, until you’re just sorting single items (already sorted by definition). 2) Merge step: combining two already-sorted lists can be done efficiently in linear time by walking through them once. This leads to a guaranteed time cost of O(n log n): - The “log n” comes from how many times you can halve n until you get to 1 (the number of splitting levels). - The “n” comes from the fact that at each level of splitting, the total work done merging across all sublists adds up to about n comparisons/moves. It’s also stable, meaning if two items compare equal (like two people with the same last name), merge sort can preserve their original relative order—an underrated superpower when sorting by multiple keys (e.g., sort by last name, then by first name without scrambling the first-name order within each last name group).
Merge sort emerged from the early days of computing when memory was scarce and data often lived outside main memory (on tapes and disks). In 1945, mathematician John von Neumann described merge sort as a practical method for sorting large amounts of data using a clean divide-and-conquer structure. The key motivation was not just speed, but predictability and scalability: unlike some faster-on-average methods, merge sort provides a firm worst-case guarantee of O(n log n). It also adapts naturally to “external sorting,” where you can only load chunks of data into memory at once—exactly the kind of constraint early computers faced, and modern systems still face when sorting datasets larger than RAM.
Pioneered by: John von Neumann (1945)