Merge Sort

You might also like

You are on page 1of 17

Merge Sort

Sort Algorithm
What is Merge Sort?
Merge sort is a sorting algorithm based on the Divide and

conquer strategy. It works by recursively dividing the array

into two equal halves, then sort them and combine them. It

takes a time of (n logn) in the worst case.

9/4/20XX Merge Sort 2


What is Merge Sort?
The merge sort algorithm is an implementation of the divide and
conquer technique. Thus, it gets completed in three steps:

1. Divide: In this step, the array/list divides itself recursively into


sub-arrays until the base case is reached.

2. Recursively solve: Here, the sub-arrays are sorted using recursion.

3. Combine: This step makes use of the merge( ) function to combine


the sub-arrays into the final sorted array.

3
Algorithm for Merge Sort

• Step 1: Find the middle index of the array.


Middle = 1 + (last – first)/2
Step 2: Divide the array from the middle.
Step 3: Call merge sort for the first half of the array
MergeSort(array, first, middle)
Step 4: Call merge sort for the second half of the array.
MergeSort(array, middle+1, last)
Step 5: Merge the two sorted halves into a single sorted array.

9/4/20XX Presentation Title 4


Working of Merge Sort

Consider an array having the following elements: 1,6,3,2,7,5,8,4.


We need to sort this array using merge sort.
There is a total of 8 elements in the array. Thus, mid = 4.
So we will first divide this array into two arrays of size 4 as
shown:

9/4/20XX Presentation Title 7


Next, we recursively divide these arrays into further halves. The
half of 4 is 2. So, now we have 4 arrays of size 2 each.

9/4/20XX Presentation Title 8


We will keep on dividing into further halves until we have
reached an array length of size 1 as shown:

9/4/20XX Presentation Title 9


After this, we have the combining step. We will compare each
element with its consecutive elements and arrange them in a
sorted manner.

9/4/20XX Presentation Title 10


In the next iteration, we will compare two arrays and sort them
as shown:

9/4/20XX Presentation Title 11


Finally, we will compare the elements of the two arrays each of
size 4 and we will get our resultant sorted array as shown:

9/4/20XX Presentation Title 12


Characteristics of 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

more than O(logn).

9/4/20XX Presentation Title 13


Applications of Merge Sort

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).

9/4/20XX Presentation Title 14


Applications of Merge Sort

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.

9/4/20XX Presentation Title 15


Drawbacks of 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.

9/4/20XX Presentation Title 16


Conclusion

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.

9/4/20XX Presentation Title 17


Mohamad Jabar
Refrence Mhamad Talib

https://techvidvan.com/tutorials/mer
ge-sort/

Thank you

You might also like