0% found this document useful (0 votes)
3 views

Module 2 Part D

Merge Sort is a Divide and Conquer algorithm that recursively divides an input array into two halves, sorts each half, and then merges the sorted halves. Its time complexity is consistently O(n log n) across all cases, making it efficient for sorting. However, it has a space complexity of O(n), which may hinder performance with large datasets.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 2 Part D

Merge Sort is a Divide and Conquer algorithm that recursively divides an input array into two halves, sorts each half, and then merges the sorted halves. Its time complexity is consistently O(n log n) across all cases, making it efficient for sorting. However, it has a space complexity of O(n), which may hinder performance with large datasets.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Amity School of Engineering and Technology

Merge Sort

B. Tech-CSE, Semester: Vth


Course: ADA
Learning Objectives
 Introduction
 Algorithm Merge-Sort
 Complexity analysis
 Characteristics
 Summary

2
Introduction
• Merge sort algorithm is a classic example of divide and
conquer
• To sort an array, recursively, sort its left and right halves
separately and then merge them
• The fundamental operation in this algorithm is merging
two sorted lists
• Merge sort repeatedly breaks down a list into several
sub-lists until each sub-list consists of a single element
and merging those sub-lists in a manner that results into
a sorted list.

3
Introduction

• Split array A[0..n-1] in two about equal


halves and make copies of each half in
arrays B and C

• Sort arrays B and C recursively

• Merge sorted arrays B and C into array A

4
Algorithm Merge-Sort

Input: A set S of n elements.


Output: the sorted sequence of the inputs in
ascending order.
Step 1: If |S|2, solve it directly
Step 2: Recursively apply this algorithm to solve
the left half part and right half part of S, and the
results are stored in S1 and S2, respectively.
Step 3: Perform the two-way merge scheme on
S1 and S2.

5-5
Merge sort
Pseudo code of Merge sort[1]
Merge sort Cont…
8 3 2 9 7 1 5 4
Mergesort
8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

3 8 2 9 1 7 4 5

2 3 8 9 1 4 5 7

1 2 3 4 5 7 8 9

Fig 1: Merge sort representation [1]


Analysis of Complexity

9
Analysis
As we know that

Master theorem T(n) = aT(n/b) + f(n)

There are following three cases:


1. If f(n) = Θ(nc) where c < Logba then T(n) = Θ(nLogba)
2. If f(n) = Θ(nc) where c = Logba then T(n) = Θ(ncLog n)
3.If f(n) = Θ(nc) where c > Logba then T(n) = Θ(f(n))

10
Analysis

Time complexity of Merge Sort can be written as


T(n) = 2T(n/2) + D(n) + C(n).
Here D(n)=1 (Why?)
C(n)= n (Why?)

Here logba is also 1. So the solution is Θ(n logn)


[by case II of master theorem’s method]

11
Characteristics
Important Characteristics of Merge Sort [2]:
•Merge Sort is useful for sorting linked lists.
•Merge Sort is a stable sort which means that the same
element in an array maintain their original positions with
respect to each other.
•Overall time complexity of Merge sort is O(nlogn). It is
more efficient as it is in worst case also the runtime is
O(nlogn)
•The space complexity of Merge sort is O(n). This means
that this algorithm takes a lot of space
and may slower down operations for the last data sets.

12
Characteristics
Characteristics of Merge Sort [3]
•Time complexity of Merge Sort is O(n*Log n) in all the 3
cases (worst, average and best) as merge sort
always divides the array in two halves and takes linear time
to merge two halves.

•It requires equal amount of additional space as the


unsorted array. Hence its not at all recommended for
searching large unsorted arrays.

•It is the best Sorting technique used for sorting Linked Lists.

13
References
[1] A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd
ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ.
All Rights Reserved.
[2] https://medium.com/karuna-sehgal
[3] https://www.studytonight.com
[4] www.codesdope.com/course/algorithms-merge-sort/

14
Summary

• Merge Sort is a Divide and Conquer


algorithm.
• It divides input array in two halves, calls
itself for the two halves and then merges
the two sorted halves.

15

You might also like