Merge Sort Algorithm

Maryann Gitonga
2 min readSep 4, 2023

--

How it works

Imagine you have a massive stack of unsorted papers, and you desperately need them to be in order. First, you take your stack and divide it into smaller, manageable piles (divide approach). As you repeat this process, you are able to review each single paper and now you can figure out which mini pile each belongs to when you merge them together.

So, you start merging these papers back together, forming min piles and as you repeat this process, you merge the piles into one massive, perfectly sorted stack of papers. You do this thoughtfully, making sure that every paper finds its rightful place in the stack (conquer approach).

This is Merge Sort. You break down the big problem into smaller ones, sort them out, and then blend everything back together harmoniously.

Illustration of Merge Sort: Divide & Conquer Approach

Benefits

  1. Great for Large Datasets: It is suitable when it comes to sorting huge amounts of data. It handles big datasets with ease.
  2. Easy Multitasking: the divide-and-conquer approach allows for easy parallelization. Sorting and merging of different sub-arrays can be executed concurrently, leveraging multi-core processors for improved speed.
  3. No-Memory Problem Solver: It is suitable for external sorting, when data is too large to fit entirely in memory. It efficiently handles datasets that need to be sorted using disk-based operations.

Real-life application

In search engines, merge sort algorithms are used when processing and merging search results from different sources while preserving relevance and ranking.

Simple Code Illustration

Where next?

--

--

Maryann Gitonga

A Software Engineer who loves trying out different tooling & engineering concepts in Back-end Engineering & DevOps. An explorer in the ML world too!