SlideShare una empresa de Scribd logo
1 de 26
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

Más contenido relacionado

La actualidad más candente

I. Hill climbing algorithm II. Steepest hill climbing algorithm
I. Hill climbing algorithm II. Steepest hill climbing algorithmI. Hill climbing algorithm II. Steepest hill climbing algorithm
I. Hill climbing algorithm II. Steepest hill climbing algorithmvikas dhakane
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Clock synchronization in distributed system
Clock synchronization in distributed systemClock synchronization in distributed system
Clock synchronization in distributed systemSunita Sahu
 
Traveling salesman problem
Traveling salesman problemTraveling salesman problem
Traveling salesman problemJayesh Chauhan
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and boundAbhishek Singh
 
Chess board problem(divide and conquer)
Chess board problem(divide and conquer)Chess board problem(divide and conquer)
Chess board problem(divide and conquer)RASHIARORA8
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort AlgorithmGail Carmichael
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.mohanrathod18
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 

La actualidad más candente (20)

NLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit DistanceNLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit Distance
 
I. Hill climbing algorithm II. Steepest hill climbing algorithm
I. Hill climbing algorithm II. Steepest hill climbing algorithmI. Hill climbing algorithm II. Steepest hill climbing algorithm
I. Hill climbing algorithm II. Steepest hill climbing algorithm
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
 
Clock synchronization in distributed system
Clock synchronization in distributed systemClock synchronization in distributed system
Clock synchronization in distributed system
 
Traveling salesman problem
Traveling salesman problemTraveling salesman problem
Traveling salesman problem
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Polyphase
PolyphasePolyphase
Polyphase
 
Chess board problem(divide and conquer)
Chess board problem(divide and conquer)Chess board problem(divide and conquer)
Chess board problem(divide and conquer)
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
Insertion Sort Algorithm
Insertion Sort AlgorithmInsertion Sort Algorithm
Insertion Sort Algorithm
 
String matching algorithm
String matching algorithmString matching algorithm
String matching algorithm
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 

Destacado

Destacado (20)

Merge sort
Merge sortMerge sort
Merge sort
 
Presentation-Merge Sort
Presentation-Merge SortPresentation-Merge Sort
Presentation-Merge Sort
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
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
 
Merge sort
Merge sortMerge sort
Merge sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Mergesort
MergesortMergesort
Mergesort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Quick sort
Quick sortQuick sort
Quick sort
 

Similar a Merge sort analysis and its real time applications

Stevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsStevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsJames Stevens
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 
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.pptxnikshaikh786
 
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 SortIRJET 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 AlgorithmsIRJET Journal
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm AnalysisMary 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.pdfAkashSingh625550
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmAshim Sikder
 

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

Stevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting AlgorithmsStevens-Benchmarking Sorting Algorithms
Stevens-Benchmarking Sorting Algorithms
 
Sorting
SortingSorting
Sorting
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
 
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
 
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- 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
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
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...
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
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...
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Ln liers
Ln liersLn liers
Ln liers
 
test1
test1test1
test1
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 
Unit i
Unit iUnit i
Unit i
 

Más de 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 databasesyazad dumasia
 
Classification decision tree
Classification  decision treeClassification  decision tree
Classification decision treeyazad 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: Inflationyazad dumasia
 
Groundwater contamination
Groundwater contaminationGroundwater contamination
Groundwater contaminationyazad 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 cyazad dumasia
 

Más de yazad dumasia (8)

Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib. Introduction to Pylab and Matploitlib.
Introduction to Pylab and Matploitlib.
 
Schemas for multidimensional databases
Schemas for multidimensional databasesSchemas for multidimensional databases
Schemas for multidimensional databases
 
Classification decision tree
Classification  decision treeClassification  decision tree
Classification decision tree
 
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...
 
Basic economic problem: Inflation
Basic economic problem: InflationBasic economic problem: Inflation
Basic economic problem: Inflation
 
Groundwater contamination
Groundwater contaminationGroundwater contamination
Groundwater contamination
 
Cyber crime
Cyber crimeCyber crime
Cyber crime
 
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
 

Último

MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 

Último (20)

MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 

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