Time and Space Complexities

  • Time Complexity: Merge sort has a time complexity of O(n log n) in the worst-case scenario. This is because it divides the array into halves recursively and merges them in linear time.
  • Space Complexity: Merge sort has a space complexity of O(n) due to the need for auxiliary space for merging the sub-arrays. However, in practice, it’s often considered out-of-place, meaning it requires additional space proportional to the size of the input array.


How to use Merge Sort with TypeScript ?

Merge sort follows the divide and conquer approach, which means it breaks the problem into smaller sub-problems, solves each sub-problem, and then combines the solutions to solve the original problem.

Similar Reads

How does merge sort work?

Begin with a Recursive Divide and Conquer Approach:...

Approach:

Create a recursive function named mergeSort() to divide the passed array into two parts and recursively do the same thing by calling itself again and again until the length of array becomes 0 or 1. Define another function named merge() to merge the sorted sub parts of the array and store them in a single sorted array. The merge function will merge the arrays in such a way that the smaller elements will be pushed first and larger at last. This process will repeat itself untile the whole array gets sorted and merged....

Time and Space Complexities:

...