# Comparing Merge and Quick Sorting Algorithms

Published on Wednesday, March 27, 2024 (4 months ago)

Wether you’re a beginner or a greatly experienced developer, this comparison will come in handy to find the fastest of two sorting algorithms. Namely **merge** and **quick**.

Instead of a wall-of-text, we’ll get into the nitty-gritty right away!

*Merge* Sort

The merge sorting (also commonly seen in one word `mergesort`

) algorithm was invented in *1945* by **John von Neumann**. This is based on the divide-and-conquer strategy.

The worst-case time complexity of this is $O(n\log n)$.

*Quick* Sort

`Quick sort`

is a powerful sorting algorithm that belongs to the divide-and-conquer family. It works by first selecting a pivot element from the unsorted list. Then, it rearranges the list by placing elements smaller than the pivot to its left and elements larger than the pivot to its right. This process is called partitioning. Finally, `quicksort`

recursively sorts the two sub-lists on either side of the pivot, effectively sorting the entire list.

The efficiency of `quicksort`

hinges on the choice of the pivot element. In the average case, with a randomly chosen pivot, `quicksort`

has a time complexity of $O(n \log n)$, making it one of the fastest sorting algorithms. This means the number of comparisons it takes to sort a list grows proportionally to the logarithm of the number of elements.

We first heard about `quicksort`

in *1959* (published *1961*) from the British computer scientist **Sir Charles Antony Richard Hoare**.

However, `quicksort`

’s Achilles’ heel is its worst-case performance. If the pivot element consistently ends up being the largest or smallest element in the list, the algorithm degrades to $O(n^2)$ complexity. This can happen with already sorted or reverse-sorted data.

## Comparison

We will start, with an array of `3500`

random (*hopefully* distinct) floating-point values, to test our two sorting algorithms.

Now that we have some data to test on, we want to add the algorithm for the **merge sort**. This goes as follows.

And of course the **quick sort** as well, otherwise we won’t have anything to compare against.

Now, let’s test the two against one another.

If you want to read about something else here is a collection of some different sorting algorithms. You can also navigate directly to the implementation of

merge sortVS.quick sort, this is espescially useful if you want to manipulate each of these algorithms yourself.

Running this benchmark against the **merge** sort and **quick** sort, we will notice that one is **54786.64x** faster than the other, which is … drum roll, please! The great **quick sorting algorithm**!

This means we can almost run the *quick* sort algorithm almost up to 54787 times in the time it takes *merge* sort to run once!

## Artificial Intelligence

We asked a well-known *Artificial Intelligence* (*A.I.* for short, *AI* for the lazy and *ai* for the lazier) to nickname our winner, the **quick sort**, and this shall hence on be known as ”*The Quickfire Ninja*“!

To be fair, of course, we also asked for one for our lesser speedy algorithm, the **merge sort**, which is now known as ”*The Merge Mastermind*“.

## Epilogue

Does this mean you should always choose the quick sorting algorithm when you implement a new app? Well, not exactly… Please do remember that, as with everything in life, there are pros and cons as well, so choose wisely!

There are a lot of other sorting algorithms out there. Yet more to be discovered! Who knows, you might find the next one with an immersive speed increase or magnificent memory benefits!

Thanks for reading, and I truly hope this will better your understanding of the speed of **merge** and **quick** sorting algorithms.

More posts are available on the blog.