SlideShare a Scribd company logo
1 of 9
Download to read offline
NETAJI SUBASH ENGINEERING COLLEGE
Topic:- Merge Sort
 PRESENTED BY: PRADEEP KUMAR
 SUBJECT: ADVANCED ALGORITHMS
UNIVERSITY ROLL NO. :10900220021
CLASS ROLL NO. :21
STREAM :IT
SEC :'A'
SEM:-(6TH)
Merge Sort
INTRODUCTION
Merge Sort is a Divide and
Conquer algorithm. It divides the input
array in two halves, calls itself for the two
halves and then merges the two sorted
halves.
The merge() function is used for merging
two halves. The merge(arr, l, m, r) is key
process that assumes that arr[l..m] and
arr[m+1..r] are sorted and merges the
two sorted sub-arrays into one in a sorted
manner.
Merge Sort
Algorithm
Merge Sort Algorithm works in the following
steps-
 It divides the given unsorted array into two halves- left
and right sub arrays.
 The sub arrays are divided recursively.
 This division continues until the size of each sub array
becomes 1.
 After each sub array contains only a single element,
each sub array is sorted trivially.
 Then, the above discussed merge procedure is called.
 The merge procedure combines these trivially sorted
arrays to produce a final sorted array.
 Merge Sort Code
 // A : Array that needs to be sorted
 MergeSort(A)
 {
 n = length(A)
 if n<2 return
 mid = n/2
 left = new_array_of_size(mid) // Creating temporary array for left
 right = new_array_of_size(n-mid) // and right sub arrays
 for(int i=0 ; i<=mid-1 ; ++i)
 {
 left[i] = A[i] // Copying elements from A to left
 }
 for(int i=mid ; i<=n-1 ; ++i)
 {
 right[i-mid] = A[i] // Copying elements from A to right
 }
 MergeSort(left) // Recursively solving for left sub array
 MergeSort(right) // Recursively solving for right sub array
 merge(left, right, A) // Merging two sorted left/right sub array to final array
 }
The Merge Sort Algorithm
Works As-(Example)
• The following diagram from shows the
complete merge sort process for an
example array {38, 27, 43, 3, 9, 82, 10}. If
we take a closer look at the diagram, we
can see that the array is recursively
divided in two halves till the size
becomes 1. Once the size becomes 1, the
merge processes comes into action and
starts merging arrays back till the
complete array is merged.
Time Complexity
Analysis-
 In merge sort, we
divide the array into
two (nearly) equal
halves and solve them
recursively using
merge sort only.
So we have:
Finally, we merge these two sub arrays using merge
procedure which takes Θ(n) time
If T(n) is the time required by merge sort for sorting
an array of size n, then the recurrence relation for
time complexity of merge sort is-
On solving this recurrence relation, we get
T(n) = Θ(nlogn).
Thus, time complexity of merge sort
algorithm is T(n) = Θ(nlogn).
Time complexity of Merge Sort
is Θ(nLogn) in all 3 cases (worst, average and
best) as merge sort always divides the array in
two halves and take linear time to merge two
halves.
Space Complexity Analysis-
 Merge sort uses additional memory for left and right sub
arrays.
 Hence, total Θ(n) extra memory is needed.
Some of the important properties of
merge sort algorithm are
Merge sort uses a divide and conquer paradigm for sorting.
Merge sort is a recursive sorting algorithm.
Merge sort is a stable sorting algorithm.
Merge sort is not an in-place sorting algorithm.
The time complexity of merge sort algorithm is Θ(nlogn).
The space complexity of merge sort algorithm is Θ(n).
THANK YOU

More Related Content

Similar to NETAJI SUBASH ENGINEERING COLLEGE

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
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer scienceRiazul Islam
 
Parallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
Parallel Algorithms: Sort & Merge, Image Processing, Fault ToleranceParallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
Parallel Algorithms: Sort & Merge, Image Processing, Fault ToleranceUniversity of Technology - Iraq
 
K way merging advanced data structures materials
K way merging advanced data structures materialsK way merging advanced data structures materials
K way merging advanced data structures materialsssuser7319f8
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
Design and analysis of ra sort
Design and analysis of ra sortDesign and analysis of ra sort
Design and analysis of ra sortijfcstjournal
 
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and SystemsDSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and SystemsAmr E. Mohamed
 
DATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxDATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxShivamKrPathak
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxsgrishma559
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
 
Chp-1 Quick Review of basic concepts.pdf
Chp-1 Quick Review of basic concepts.pdfChp-1 Quick Review of basic concepts.pdf
Chp-1 Quick Review of basic concepts.pdfSolomonMolla4
 

Similar to NETAJI SUBASH ENGINEERING COLLEGE (20)

Parallel algorithm in linear algebra
Parallel algorithm in linear algebraParallel algorithm in linear algebra
Parallel algorithm in linear algebra
 
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...
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Brute force method
Brute force methodBrute force method
Brute force method
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
N41049093
N41049093N41049093
N41049093
 
Parallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
Parallel Algorithms: Sort & Merge, Image Processing, Fault ToleranceParallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
Parallel Algorithms: Sort & Merge, Image Processing, Fault Tolerance
 
K way merging advanced data structures materials
K way merging advanced data structures materialsK way merging advanced data structures materials
K way merging advanced data structures materials
 
Merge sort
Merge sortMerge sort
Merge sort
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Merge sort
Merge sortMerge sort
Merge sort
 
Design and analysis of ra sort
Design and analysis of ra sortDesign and analysis of ra sort
Design and analysis of ra sort
 
Sorting
SortingSorting
Sorting
 
Chapter26
Chapter26Chapter26
Chapter26
 
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and SystemsDSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
DSP_FOEHU - MATLAB 01 - Discrete Time Signals and Systems
 
DATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxDATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptx
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Chp-1 Quick Review of basic concepts.pdf
Chp-1 Quick Review of basic concepts.pdfChp-1 Quick Review of basic concepts.pdf
Chp-1 Quick Review of basic concepts.pdf
 

Recently uploaded

US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxsomshekarkn64
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 

Recently uploaded (20)

US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptx
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 

NETAJI SUBASH ENGINEERING COLLEGE

  • 1. NETAJI SUBASH ENGINEERING COLLEGE Topic:- Merge Sort  PRESENTED BY: PRADEEP KUMAR  SUBJECT: ADVANCED ALGORITHMS UNIVERSITY ROLL NO. :10900220021 CLASS ROLL NO. :21 STREAM :IT SEC :'A' SEM:-(6TH)
  • 2. Merge Sort INTRODUCTION Merge Sort is a Divide and Conquer algorithm. It divides the input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one in a sorted manner.
  • 3. Merge Sort Algorithm Merge Sort Algorithm works in the following steps-  It divides the given unsorted array into two halves- left and right sub arrays.  The sub arrays are divided recursively.  This division continues until the size of each sub array becomes 1.  After each sub array contains only a single element, each sub array is sorted trivially.  Then, the above discussed merge procedure is called.  The merge procedure combines these trivially sorted arrays to produce a final sorted array.
  • 4.  Merge Sort Code  // A : Array that needs to be sorted  MergeSort(A)  {  n = length(A)  if n<2 return  mid = n/2  left = new_array_of_size(mid) // Creating temporary array for left  right = new_array_of_size(n-mid) // and right sub arrays  for(int i=0 ; i<=mid-1 ; ++i)  {  left[i] = A[i] // Copying elements from A to left  }  for(int i=mid ; i<=n-1 ; ++i)  {  right[i-mid] = A[i] // Copying elements from A to right  }  MergeSort(left) // Recursively solving for left sub array  MergeSort(right) // Recursively solving for right sub array  merge(left, right, A) // Merging two sorted left/right sub array to final array  }
  • 5. The Merge Sort Algorithm Works As-(Example) • The following diagram from shows the complete merge sort process for an example array {38, 27, 43, 3, 9, 82, 10}. If we take a closer look at the diagram, we can see that the array is recursively divided in two halves till the size becomes 1. Once the size becomes 1, the merge processes comes into action and starts merging arrays back till the complete array is merged.
  • 6. Time Complexity Analysis-  In merge sort, we divide the array into two (nearly) equal halves and solve them recursively using merge sort only. So we have: Finally, we merge these two sub arrays using merge procedure which takes Θ(n) time If T(n) is the time required by merge sort for sorting an array of size n, then the recurrence relation for time complexity of merge sort is- On solving this recurrence relation, we get T(n) = Θ(nlogn). Thus, time complexity of merge sort algorithm is T(n) = Θ(nlogn).
  • 7. Time complexity of Merge Sort is Θ(nLogn) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves and take linear time to merge two halves. Space Complexity Analysis-  Merge sort uses additional memory for left and right sub arrays.  Hence, total Θ(n) extra memory is needed.
  • 8. Some of the important properties of merge sort algorithm are Merge sort uses a divide and conquer paradigm for sorting. Merge sort is a recursive sorting algorithm. Merge sort is a stable sorting algorithm. Merge sort is not an in-place sorting algorithm. The time complexity of merge sort algorithm is Θ(nlogn). The space complexity of merge sort algorithm is Θ(n).