Professional Documents
Culture Documents
Merge Sort
Merge Sort
Merge Sort
Sort Algorithm
What is Merge Sort?
Merge sort is a sorting algorithm based on the Divide and
into two equal halves, then sort them and combine them. It
3
Algorithm for Merge Sort
1. External sorting algorithm: Merge sort can be used when the data size is more than the RAM
size. In such a case, whole data cannot come into RAM at once. Thus, data is loaded into the
RAM in small chunks and the rest of the data resides in the secondary memory.
2. Non-inplace sorting algorithm: Merge sort is not an inplace sorting technique as its space
complexity is O(n). Inplace or space-efficient algorithms are those whose space complexity is not
1. To sort linked lists in O(n logn) time: In the case of a linked list, we can insert the
element in the middle or anywhere in between the linked list with time complexity of
O(1). If we use merge sort to sort such a linked list, we can do so with the space
complexity of O(1). Also, we cannot do random access in a linked list and merge sort also
does not work well in case of random access. Thus, merge sort is the best algorithm to
sort a linked list with the complexity of O(n logn).
2. Inversion count problem: Merge sort helps to solve this problem by telling the
number of inversion pairs in an unsorted array. The inversion count problem
tells how many pairs need to be swapped in order to get a sorted array. Merge
sort works best for solving this problem.
3. External sorting: Merge sort is an external sorting technique. Thus, if we have
data of 1GB but the available RAM size is 500MB, then we will use merge sort.
•Merge sort is not a space-efficient algorithm. It makes use of an extra O(n) space.
•In the case of smaller input size, merge sort works slower in comparison to other sorting
techniques.
•If the data is already sorted, merge sort will be a very expensive algorithm in terms of
time and space. This is because it will still traverse the whole array and perform all the
operations.
Merge sort is one of the most widely used algorithms in data structures. Although it is not
a space-efficient algorithm, its time complexity is of the order O(n logn) which is better
than most of the sorting algorithms. Whenever we have an input size larger than the RAM
size, we use merge sort. Thus, merge sort is very well suited for larger datasets.
In this article, we have studied what is merge sort, how it works, its applications,
drawbacks as well as its implementation in various programming languages.
https://techvidvan.com/tutorials/mer
ge-sort/
Thank you