Mergesort

Complexity: O(n log n). A very good explanation of merge sort by Rob Edwards. Is stable but can't be done in-place.

Basic algorithm

  • divide array in to two halves

  • recursively sort each half

    • recursively start dividing the first half, when you get into two

  • merge two halfs

Solution

Here is a solution I was able to develop. It could be optimized, we create temp array for every merge, it could be created only once at the beginning.

Here is another implementation.

Here is another implementation. I have put it here just as a reference, but I don't like it.

Last updated

Was this helpful?