SlideShare a Scribd company logo
Merge Sort Analysis
and its Real-Time
Applications
GROUP-4
BE-3RD YEAR, SEM-5TH
Sarvajanik College of Engineering & Technology
Department of Computer Engineering (Shift-1)
1
140420107014 DUDHWALA FRANCY KALPESH
140420107015 DUMASIA YAZAD HOMYAR
140420107016 GAJJAR KARAN DEVENDRABHAI
140420107018 GOTAWALA VATSAL YAGNESH .
2
Introduction to sorting
Introduction to merge sort algorithm
Complexity analysis of merge sort algorithm
Real-time application
3CONTENTS : A GLIMPSE
OF WHAT IS TO COME
Introduction to sorting
 Sorting refers to arranging a set of data in some logical order.
 For ex. A telephone directory can be considered as a list where each record has
three fields - name, address and phone number.
Being unique, phone number can work as a key to locate any record in the
list.
 Sorting is among the most basic problems in algorithm design.
 We are given a sequence of items, each associated with a given key value. And
the problem is to rearrange the items so that they are in an increasing(or
decreasing) order by key.
 The methods of sorting can be divided into two categories:
Internal Sorting
External Sorting
4
Internal Sorting
If all the data that is to be sorted can be adjusted at a time in main memory, then internal
sorting methods are used
•External Sorting
When the data to be sorted can’t be accommodated in the memory at the same time and
some has to be kept in auxiliary memory, then external sorting methods are used.
NOTE: We will only consider External sorting
5
Stable and Not Stable Sorting
 If a sorting algorithm, after sorting the contents, does not change the sequence of similar
content in which they appear, it is called stable sorting.
 If a sorting algorithm, after sorting the contents, changes the sequence of similar content in
which they appear, it is called unstable sorting.
6
Efficiency of Sorting Algorithm
 The complexity of a sorting algorithm measures the running time of a function
in which n number of items are to be sorted.
 The choice of sorting method depends on efficiency considerations for
different problems.
 Tree most important of these considerations are:
 The length of time spent by programmer in coding a particular sorting program.
 Amount of machine time necessary for running the program.
 The amount of memory necessary for running the program.
7
Efficiency of Sorting Algorithm
 Various sorting methods are analyzed in the cases like – best case, worst case or
average case.
 Most of the sort methods we consider have requirements that range from O(n
logn) to O(n2).
 A sort should not be selected only because its sorting time is 0(nlogn); the relation
of the file size n and the other factors affecting the actual sorting time must be
considered.
 Determining the time requirement of sorting technique is to actually run the
program and measure its efficiency.
 Once a particular sorting technique is selected the need is to make the program as
efficient as possible.
 Any improvement in sorting time significantly affect the overall efficiency and saves
a great deal of computer time.
8
Efficiency of Sorting Algorithm
 Space constraints are usually less important than time considerations.
 The reason for this can be, as for most sorting programs, the amount of
space needed is closer to 0(n) than to 0(n2)
 The second reason is that, if more space is required, it can almost always
be found in auxiliary storage.
9
Introduction to merge sort algorithm
10
 Divide-and-conquer, breaks a problem into sub problems that are similar to the
original problem, recursively solves the sub problems, and finally combines the
solutions to the sub problems to solve the original problem.
 Think of a divide-and-conquer algorithm as having three parts:
 Divide the problem into a number of sub-problems that are smaller instances of the same
problem.
 Conquer the sub-problems by solving them recursively. If they are small enough, solve the
sub-problems as base cases.
 Combine the solutions to the sub-problems into the solution for the original problem.
 Because we're using divide-and-conquer to sort, we need to decide what our sub problems
are going to be.
Divide-and-conquer algorithms
11Problem
divide
Sub problem Sub problem
Conquer
Solve
sub-problem
Solve
sub-problem
Solution
to
Sub-problem
Solution
to
Sub-problem
Solution to problem
Combine
Merge sort algorithm 12
Merge sort is a sorting technique based on divide and conquer
technique that was invented by John von Neumann in 1945.
Merge sort work on Two basic principle :
• Sorting smaller list is faster than sorting larger list.
• Combining two sorted sub lists is faster than
of two unsorted list.
Working of merge sort 13
Merge sort works in three stage:
• Divide : Merge sort first divides the list into equal halves and then
combines them in a sorted manner.
• Conquer : Then sort the sub-lists
• Combine : After sorting merge all sub-lists into single list.
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
2618 632 1543 19
18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
18 26 326 15 43 1 9
6 18 26 32 1 9 15 43
1 6 9 15 18 26 32 43
18 26
18 26
18 26
32
32
6
6
32 6
18 26 32 6
43
43
15
15
43 15
9
9
1
1
9 1
43 15 9 1
18 26 32 6 43 15 9 1
18 26 632
626 3218
1543 19
1 915 43
16 9 1518 26 32 43
Original Sequence Sorted Sequence 14
Working of merge sort
43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6
15
16
kn
kn/2 kn/2
kn/4 kn/4 kn/4 kn/4
1 1 1 1 1 1 1 1
kn
kn
kn
kn
Log2 n
Total : Log2 n + kn
Algorithm for merge sort :
17
Algorithm MERGE_SORT(A,1,n)
//A is array of size n
if low < high then
mid  floor ( low + high ) / 2
MERGE_SORT(A , low , mid)
MERGE_SORT(A , mid+1 , high)
COMBINE(A , low, mid, high)
end
Algorithm COMBINE(A , low , mid , high)
L1  mid – low +1
L2  high – mid
for i  1 to L1 do
LEFT[i]  A [ low + i -1 ]
end
for j  1 to L2 do
RIGHT[ i ]  A[ mid + j ]
end
LEFT [ L1 + 1] ∞
RIGHT [ L2 + 1]  ∞
i  1 , j  1
for k  low to high do
if LEFT [ i ]  RIGHT [ j ] then
A[ k ]  LEFT [ i ]
i  i +1
else
A []  RIGHT []
j = j + 1
end
end
end
Ex: sorting the list using merge sort algorithm.
18
33 22 44 00 99 88 11
0 1 42 653
33 22 44 00
99 88 11
1 11
33 22 44 00
33 22 44 00
22 33 00 44
00 22 33 44
99 88 11
8899
88 99
11
11
11 88 99
33 22 44 00 99 88 11
2 6
3
10
5
4 7
9
8
12
17
16
18
13
15
14
p r
Merge sort works as follow:
MERGE_SORT(A , p , r):
if p<r then, q=( r + p)/2
MERGE_SORT(A , p , q)
MERGE_SORT(A , q+1 , r)
COMBINE(A, p , q , r)
end
19
Step- 0 : MERGE_SORT(A , 0 , 6)
Step- 1 : MERGE_SORT(A , 0 , 3) Step-11 : MERGE_SORT(A , 4 , 6)
Step- 2 : MERGE_SORT(A , 0 , 1) Step-12 : MERGE_SORT(A , 4 , 5)
Step- 3 : MERGE_SORT(A , 0 , 0) Step-13 : MERGE_SORT(A , 4 , 4)
Step- 4 : MERGE_SORT(A , 1 , 1) Step-14 : MERGE_SORT(A , 5 , 5)
Step- 5 : COMBINE(A , 0 , 0 , 1) Step-15 : COMBINE(A , 4 , 4 , 4 , 5)
Step- 6 : MERGE_SORT(A , 2 , 3) Step-16 : MERGE_SORT(A , 6 , 6)
Step-7 : MERGE_SORT(A , 2, 2) Step-17 : COMBINE(A , 4 , 5 , 6)
Step-8 : MERGE_SORT(A , 3 , 3) Step-18 : COMBINE(A , 0 , 3 ,6)
Step-9 : COMBINE(A , 2 , 2 , 3)
Step-10 : COMBINE(A , 0 , 1 , 3)
20
Example base on previous method to sorting the list
using merge sort algorithm.
Complexity analysis of merge sort algorithm
Divide : This step computes the middle of the sub array, which
takes constant time . Thus, D(n) = θ(1).
Conquer : We recursively sole two sub problems, each of size (n/2)
, which contributes 2T(n/2) to the running time.
Combine: Combine procedure on an n-element sub array takes
times θ(n).so C(n) = θ(n).
T(n)=
21
0 ,if n <=1
T(n/2) + T(n/2) + D(n) + C(n) , else
22
T(n) = 2 * T(n/2) + θ(n) + θ(1)
T(n) = 2 * T(n/2) + n
T(n/2) = 2 * ( 2 * T(n/4) + n/2 )
T(n) = 2 * ( 2 * T(n/2) + n/2 ) + n
= 22 * T(n/ 22) + 2n
.
.
.
.
=2k T(n/ 2k)+ kn
Suppose, n = 2k so k = log2 n
T(2k) = n* T(n/n) + log2 n * n
=n *T(1) +n* log2 n
But T(1)=0
T(n)= O(n* log2 n )
23
Weather list is already sorted , inverse sorted or randomly distributed , all three steps must be
performed.
Merge sort cannot identify if list is sorted. So numbers of comparisons are same for 3 case.
Worst case of merge sort takes les time compared to insertion sort , selection sort & bubble sort .
However , best case of insertion sort (O(n)) beats all three cases of merge sort(O(nlog2n))
Best case Average case Worst case
O(nlog2n) O(nlog2n) O(nlog2n)
Property of merge sort :
• Not Adaptive : Running time doesn’t change with data pattern.
• Stable/ Unstable : Both implementations are possible .
• Not Incremental : Does not sort one by one element in each pass.
• Not online : Need all data to be in memory at the time of sorting.
• Not in place : It need O(n) extra space to sort two sub list of
size(n/2).
24
Real-time application
The e-commerce application
Have you ever noticed on any e-commerce website, they have this section of "You
might like", they have maintained an array for all the user accounts and then
whichever has the least number of inversion with your array of choices, they start
recommending what they have bought or they like. I am not going into the time and
space complexity details of the algorithm. Obviously there are a lot of ways of doing
this and this is one of them.
And some e-commerce website like policybazaar.com , trivago.com etc. use there
search engine for collecting data from other website to provide minimum cost of
booking hotel room or purchasing product .
25
26
Ad

More Related Content

What's hot (20)

Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
Shubham Dwivedi
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Marge Sort
Marge SortMarge Sort
Marge Sort
Ankit92Chitnavis
 
Merge sort
Merge sortMerge sort
Merge sort
lakshitha perera
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Polyphase
PolyphasePolyphase
Polyphase
Adrita Chakraborty
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
Merge sort
Merge sortMerge sort
Merge sort
Vidushi Pathak
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
FarihaHabib123
 
Implementing Merge Sort
Implementing Merge SortImplementing Merge Sort
Implementing Merge Sort
smita gupta
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
Job sequencing with Deadlines
Job sequencing with DeadlinesJob sequencing with Deadlines
Job sequencing with Deadlines
YashiUpadhyay3
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
mandlapure
 
Sorting
SortingSorting
Sorting
Ashim Lamichhane
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Binary Search
Binary SearchBinary Search
Binary Search
kunj desai
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
jayavignesh86
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
QAU ISLAMABAD,PAKISTAN
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
Tech_MX
 

Viewers also liked (20)

Presentation-Merge Sort
Presentation-Merge SortPresentation-Merge Sort
Presentation-Merge Sort
Md Showrov Ahmed
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained
Mohit Tare
 
Merge sort
Merge sortMerge sort
Merge sort
Chusnul Khotimah
 
Merge sort
Merge sortMerge sort
Merge sort
Maher Alshammari
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk through
Yoshi Watanabe
 
Merge sort
Merge sortMerge sort
Merge sort
Srikrishnan Suresh
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
almaqboli
 
Merge sort
Merge sortMerge sort
Merge sort
Kumar
 
Merge sort
Merge sortMerge sort
Merge sort
Sindhoo Oad
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
Dorina Isaj
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
Brett Duncan
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
Shakila Mahjabin
 
Mergesort
MergesortMergesort
Mergesort
luzenith_g
 
Selection sort
Selection sortSelection sort
Selection sort
Jay Patel
 
Selection sort
Selection sortSelection sort
Selection sort
smlagustin
 
Quick sort
Quick sortQuick sort
Quick sort
Jehat Hassan
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
multimedia9
 
Quick Sort
Quick SortQuick Sort
Quick Sort
priyankanaidu6
 
Ad

Similar to Merge sort analysis and its real time applications (20)

Stevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsStevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting Algorithms
James Stevens
 
Sorting
SortingSorting
Sorting
invertis university
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
RenalthaPujaBagaskar
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
yasser3omr
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
SushantRaj25
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
KamalAlbashiri
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
Eman magdy
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
Study on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining SortStudy on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining Sort
IRJET Journal
 
Data Structures & Algorithms - Lecture 1
Data Structures & Algorithms - Lecture 1Data Structures & Algorithms - Lecture 1
Data Structures & Algorithms - Lecture 1
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 
A Variant of Modified Diminishing Increment Sorting: Circlesort and its Perfo...
A Variant of Modified Diminishing Increment Sorting: Circlesort and its Perfo...A Variant of Modified Diminishing Increment Sorting: Circlesort and its Perfo...
A Variant of Modified Diminishing Increment Sorting: Circlesort and its Perfo...
CSCJournals
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Data structure Merge Sort implementation
Data structure Merge Sort implementationData structure Merge Sort implementation
Data structure Merge Sort implementation
michaelose247
 
Algorithms Analysis & Design - Lecture 8
Algorithms Analysis & Design - Lecture 8Algorithms Analysis & Design - Lecture 8
Algorithms Analysis & Design - Lecture 8
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Ln liers
Ln liersLn liers
Ln liers
Shodhan Kini
 
test1
test1test1
test1
Shodhan Kini
 
Stevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsStevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting Algorithms
James Stevens
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
yasser3omr
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
Eman magdy
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
Study on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining SortStudy on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining Sort
IRJET Journal
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 
A Variant of Modified Diminishing Increment Sorting: Circlesort and its Perfo...
A Variant of Modified Diminishing Increment Sorting: Circlesort and its Perfo...A Variant of Modified Diminishing Increment Sorting: Circlesort and its Perfo...
A Variant of Modified Diminishing Increment Sorting: Circlesort and its Perfo...
CSCJournals
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
Tosin Amuda
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Data structure Merge Sort implementation
Data structure Merge Sort implementationData structure Merge Sort implementation
Data structure Merge Sort implementation
michaelose247
 
Ad

More from yazad dumasia (8)

Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib.
yazad dumasia
 
Schemas for multidimensional databases
Schemas for multidimensional databasesSchemas for multidimensional databases
Schemas for multidimensional databases
yazad dumasia
 
Classification decision tree
Classification  decision treeClassification  decision tree
Classification decision tree
yazad dumasia
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
Basic economic problem: Inflation
Basic economic problem: InflationBasic economic problem: Inflation
Basic economic problem: Inflation
yazad dumasia
 
Groundwater contamination
Groundwater contaminationGroundwater contamination
Groundwater contamination
yazad dumasia
 
Cyber crime
Cyber crimeCyber crime
Cyber crime
yazad dumasia
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
yazad dumasia
 
Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib.
yazad dumasia
 
Schemas for multidimensional databases
Schemas for multidimensional databasesSchemas for multidimensional databases
Schemas for multidimensional databases
yazad dumasia
 
Classification decision tree
Classification  decision treeClassification  decision tree
Classification decision tree
yazad dumasia
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
Basic economic problem: Inflation
Basic economic problem: InflationBasic economic problem: Inflation
Basic economic problem: Inflation
yazad dumasia
 
Groundwater contamination
Groundwater contaminationGroundwater contamination
Groundwater contamination
yazad dumasia
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
yazad dumasia
 

Recently uploaded (20)

Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025Adobe Master Collection CC Crack Advance Version 2025
Adobe Master Collection CC Crack Advance Version 2025
kashifyounis067
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Douwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License codeDouwan Crack 2025 new verson+ License code
Douwan Crack 2025 new verson+ License code
aneelaramzan63
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 

Merge sort analysis and its real time applications

  • 1. Merge Sort Analysis and its Real-Time Applications GROUP-4 BE-3RD YEAR, SEM-5TH Sarvajanik College of Engineering & Technology Department of Computer Engineering (Shift-1) 1
  • 2. 140420107014 DUDHWALA FRANCY KALPESH 140420107015 DUMASIA YAZAD HOMYAR 140420107016 GAJJAR KARAN DEVENDRABHAI 140420107018 GOTAWALA VATSAL YAGNESH . 2
  • 3. Introduction to sorting Introduction to merge sort algorithm Complexity analysis of merge sort algorithm Real-time application 3CONTENTS : A GLIMPSE OF WHAT IS TO COME
  • 4. Introduction to sorting  Sorting refers to arranging a set of data in some logical order.  For ex. A telephone directory can be considered as a list where each record has three fields - name, address and phone number. Being unique, phone number can work as a key to locate any record in the list.  Sorting is among the most basic problems in algorithm design.  We are given a sequence of items, each associated with a given key value. And the problem is to rearrange the items so that they are in an increasing(or decreasing) order by key.  The methods of sorting can be divided into two categories: Internal Sorting External Sorting 4
  • 5. Internal Sorting If all the data that is to be sorted can be adjusted at a time in main memory, then internal sorting methods are used •External Sorting When the data to be sorted can’t be accommodated in the memory at the same time and some has to be kept in auxiliary memory, then external sorting methods are used. NOTE: We will only consider External sorting 5
  • 6. Stable and Not Stable Sorting  If a sorting algorithm, after sorting the contents, does not change the sequence of similar content in which they appear, it is called stable sorting.  If a sorting algorithm, after sorting the contents, changes the sequence of similar content in which they appear, it is called unstable sorting. 6
  • 7. Efficiency of Sorting Algorithm  The complexity of a sorting algorithm measures the running time of a function in which n number of items are to be sorted.  The choice of sorting method depends on efficiency considerations for different problems.  Tree most important of these considerations are:  The length of time spent by programmer in coding a particular sorting program.  Amount of machine time necessary for running the program.  The amount of memory necessary for running the program. 7
  • 8. Efficiency of Sorting Algorithm  Various sorting methods are analyzed in the cases like – best case, worst case or average case.  Most of the sort methods we consider have requirements that range from O(n logn) to O(n2).  A sort should not be selected only because its sorting time is 0(nlogn); the relation of the file size n and the other factors affecting the actual sorting time must be considered.  Determining the time requirement of sorting technique is to actually run the program and measure its efficiency.  Once a particular sorting technique is selected the need is to make the program as efficient as possible.  Any improvement in sorting time significantly affect the overall efficiency and saves a great deal of computer time. 8
  • 9. Efficiency of Sorting Algorithm  Space constraints are usually less important than time considerations.  The reason for this can be, as for most sorting programs, the amount of space needed is closer to 0(n) than to 0(n2)  The second reason is that, if more space is required, it can almost always be found in auxiliary storage. 9
  • 10. Introduction to merge sort algorithm 10  Divide-and-conquer, breaks a problem into sub problems that are similar to the original problem, recursively solves the sub problems, and finally combines the solutions to the sub problems to solve the original problem.  Think of a divide-and-conquer algorithm as having three parts:  Divide the problem into a number of sub-problems that are smaller instances of the same problem.  Conquer the sub-problems by solving them recursively. If they are small enough, solve the sub-problems as base cases.  Combine the solutions to the sub-problems into the solution for the original problem.  Because we're using divide-and-conquer to sort, we need to decide what our sub problems are going to be. Divide-and-conquer algorithms
  • 11. 11Problem divide Sub problem Sub problem Conquer Solve sub-problem Solve sub-problem Solution to Sub-problem Solution to Sub-problem Solution to problem Combine
  • 12. Merge sort algorithm 12 Merge sort is a sorting technique based on divide and conquer technique that was invented by John von Neumann in 1945. Merge sort work on Two basic principle : • Sorting smaller list is faster than sorting larger list. • Combining two sorted sub lists is faster than of two unsorted list.
  • 13. Working of merge sort 13 Merge sort works in three stage: • Divide : Merge sort first divides the list into equal halves and then combines them in a sorted manner. • Conquer : Then sort the sub-lists • Combine : After sorting merge all sub-lists into single list.
  • 14. 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 2618 632 1543 19 18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1 18 26 326 15 43 1 9 6 18 26 32 1 9 15 43 1 6 9 15 18 26 32 43 18 26 18 26 18 26 32 32 6 6 32 6 18 26 32 6 43 43 15 15 43 15 9 9 1 1 9 1 43 15 9 1 18 26 32 6 43 15 9 1 18 26 632 626 3218 1543 19 1 915 43 16 9 1518 26 32 43 Original Sequence Sorted Sequence 14 Working of merge sort
  • 15. 43 15 9 1 22 26 19 55 37 43 99 2 18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2 18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2 18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2 18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2 18 26 32 6 15
  • 16. 16 kn kn/2 kn/2 kn/4 kn/4 kn/4 kn/4 1 1 1 1 1 1 1 1 kn kn kn kn Log2 n Total : Log2 n + kn
  • 17. Algorithm for merge sort : 17 Algorithm MERGE_SORT(A,1,n) //A is array of size n if low < high then mid  floor ( low + high ) / 2 MERGE_SORT(A , low , mid) MERGE_SORT(A , mid+1 , high) COMBINE(A , low, mid, high) end Algorithm COMBINE(A , low , mid , high) L1  mid – low +1 L2  high – mid for i  1 to L1 do LEFT[i]  A [ low + i -1 ] end for j  1 to L2 do RIGHT[ i ]  A[ mid + j ] end LEFT [ L1 + 1] ∞ RIGHT [ L2 + 1]  ∞ i  1 , j  1 for k  low to high do if LEFT [ i ]  RIGHT [ j ] then A[ k ]  LEFT [ i ] i  i +1 else A []  RIGHT [] j = j + 1 end end end
  • 18. Ex: sorting the list using merge sort algorithm. 18 33 22 44 00 99 88 11 0 1 42 653 33 22 44 00 99 88 11 1 11 33 22 44 00 33 22 44 00 22 33 00 44 00 22 33 44 99 88 11 8899 88 99 11 11 11 88 99 33 22 44 00 99 88 11 2 6 3 10 5 4 7 9 8 12 17 16 18 13 15 14 p r
  • 19. Merge sort works as follow: MERGE_SORT(A , p , r): if p<r then, q=( r + p)/2 MERGE_SORT(A , p , q) MERGE_SORT(A , q+1 , r) COMBINE(A, p , q , r) end 19 Step- 0 : MERGE_SORT(A , 0 , 6) Step- 1 : MERGE_SORT(A , 0 , 3) Step-11 : MERGE_SORT(A , 4 , 6) Step- 2 : MERGE_SORT(A , 0 , 1) Step-12 : MERGE_SORT(A , 4 , 5) Step- 3 : MERGE_SORT(A , 0 , 0) Step-13 : MERGE_SORT(A , 4 , 4) Step- 4 : MERGE_SORT(A , 1 , 1) Step-14 : MERGE_SORT(A , 5 , 5) Step- 5 : COMBINE(A , 0 , 0 , 1) Step-15 : COMBINE(A , 4 , 4 , 4 , 5) Step- 6 : MERGE_SORT(A , 2 , 3) Step-16 : MERGE_SORT(A , 6 , 6) Step-7 : MERGE_SORT(A , 2, 2) Step-17 : COMBINE(A , 4 , 5 , 6) Step-8 : MERGE_SORT(A , 3 , 3) Step-18 : COMBINE(A , 0 , 3 ,6) Step-9 : COMBINE(A , 2 , 2 , 3) Step-10 : COMBINE(A , 0 , 1 , 3)
  • 20. 20 Example base on previous method to sorting the list using merge sort algorithm.
  • 21. Complexity analysis of merge sort algorithm Divide : This step computes the middle of the sub array, which takes constant time . Thus, D(n) = θ(1). Conquer : We recursively sole two sub problems, each of size (n/2) , which contributes 2T(n/2) to the running time. Combine: Combine procedure on an n-element sub array takes times θ(n).so C(n) = θ(n). T(n)= 21 0 ,if n <=1 T(n/2) + T(n/2) + D(n) + C(n) , else
  • 22. 22 T(n) = 2 * T(n/2) + θ(n) + θ(1) T(n) = 2 * T(n/2) + n T(n/2) = 2 * ( 2 * T(n/4) + n/2 ) T(n) = 2 * ( 2 * T(n/2) + n/2 ) + n = 22 * T(n/ 22) + 2n . . . . =2k T(n/ 2k)+ kn Suppose, n = 2k so k = log2 n T(2k) = n* T(n/n) + log2 n * n =n *T(1) +n* log2 n But T(1)=0 T(n)= O(n* log2 n )
  • 23. 23 Weather list is already sorted , inverse sorted or randomly distributed , all three steps must be performed. Merge sort cannot identify if list is sorted. So numbers of comparisons are same for 3 case. Worst case of merge sort takes les time compared to insertion sort , selection sort & bubble sort . However , best case of insertion sort (O(n)) beats all three cases of merge sort(O(nlog2n)) Best case Average case Worst case O(nlog2n) O(nlog2n) O(nlog2n)
  • 24. Property of merge sort : • Not Adaptive : Running time doesn’t change with data pattern. • Stable/ Unstable : Both implementations are possible . • Not Incremental : Does not sort one by one element in each pass. • Not online : Need all data to be in memory at the time of sorting. • Not in place : It need O(n) extra space to sort two sub list of size(n/2). 24
  • 25. Real-time application The e-commerce application Have you ever noticed on any e-commerce website, they have this section of "You might like", they have maintained an array for all the user accounts and then whichever has the least number of inversion with your array of choices, they start recommending what they have bought or they like. I am not going into the time and space complexity details of the algorithm. Obviously there are a lot of ways of doing this and this is one of them. And some e-commerce website like policybazaar.com , trivago.com etc. use there search engine for collecting data from other website to provide minimum cost of booking hotel room or purchasing product . 25
  • 26. 26