Mergesort

Mergesort is an extremely efficient sorting algorithm, compared to selection and insertion. Mergesort utilizes recursion to achieve efficient sorting of lists.

How it works

Mergesort uses what is known as a "Divide and Conquer" strategy. This means mergesort divides the problem into smaller parts until the array length is equal to one. Then it merges together adjacent arrays and sorts them until the whole list is sorted.

Lets see this in action:

Mergesort Example

Here is another example:

Mergesort Example

Mergesort in Java

Here is what mergesort looks like in Java:

Here is what this looks like in the editor:

Mergesort Example In Editor

Extra Notes

It is important to know that if you utilize mergesort you will need to have extra space for temporary storage.

Mergesort is also recursive, meaning it calls upon itself.

In a more advanced setting we can observe that the runtime complexity of mergesort is O(nlogn). This means that mergesort is a linear (n), and the operation occurs in log(n) steps.

Last updated

Was this helpful?