Module 2 Part D
Module 2 Part D
Merge Sort
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
4
Algorithm Merge-Sort
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
9
Analysis
As we know that
10
Analysis
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 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
15