SlideShare a Scribd company logo
3
Most read
4
Most read
6
Most read
Merge Sort
Data Structures and Algorithms
Content
● What is Merge Sort?
● How it happens?
● Algorithm
● Implementation
● Complexity
● Comparison with other sort algorithms
● Advantages and Disadvantages
What is Merge Sort?
3
What is merging?
● Mixing two array elements in ascending order to
produce a third array in ascending.
Ex:
12
15
45
10
22
30
10
12
15
22
30
45
Array A Array B
Array C
» Merge sort is a sorting technique based on divide and
conquer technique with worst-case time complexity
being O(n log n)
» In computer science, merge sort is an efficient, most
respected ,general-purpose and comparison-based
sorting algorithm
» In simply, merge sort first divides the array into equal
halves and then combines them in a sorted manner
4
5
» To merge two sorted arrays,firstly we index both arrays
starting at zero,where the smallest element is located.
» Comparing the elements at each index,we choose the smaller
element,put it into the array that we are merging into.
» Increment the index of the smaller
element.
» By this method,we continually select
the next smallest element from the
two arrays and merge them into
sorted array.
How it happens?
Divide
Problem is decomposed into one or more sub problems
Conquer
Every sub problem is solved recursively
Combine
Merge the solutions of subproblems to
create the solution of the original problem
6
How it happens cont;
Ex:
Array Name : A
Starting index : p
Ending index : r
Array : A[p..r]
7
How it happens cont;
Divide
Mid point of p and r : q
the subarrays : A[p..q] and A[q+1, r]
Conquer
Sorting subarrays A[p..q] and A[q+1, r] till the base case is
reached
Combine
Combining two sorted subarrays A[p..q] and A[q+1, r]
8
9
Algorithm MergeSort(l,h){
if(l<h){
mid=(l+h)/2;
MergeSort(l,mid);
MergeSort((mid+1),h);
Merge(l,mid,h);
}
}
4 1 3 47
0 1 2 43
Low
Mid
High
l = low , h = high
2
5
6
0 1 2 3
8 5
76
9
Algorithm
10
4 1 3
4
2
5
6
0 1 2 3
8 5
76
7
0,7
0,3 4,7
0,1 2,3 4,5 6,7
0,0 1,1 2,2 3,3 4,4 5,5 6,6 7,7
Split
Merge
1 2
3
4 5
6
7
merging steps
public class MergeSortEasy {
private static void mergesort(int low, int high){
if(low < high){
int middle = (high + low) / 2;
mergesort(low, middle);
mergesort(middle + 1, high);
merge(low, middle, high);
}
}
11
Implementation
12
private static void merge(int low, int middle, int high){
for(int i = low; i <= high; i++){
helper[i] = numbers[i];
}
int i = low;
int j = middle + 1;
int k = low;
13
while(i <= middle && j <= high){
if(helper[i] <= helper[j]){
numbers[k] = helper[i];
i++;
} else {
numbers[k] = helper[j];
j++;
}
k++;
}
14
while( i <= middle){
numbers[k] = helper[i];
k++;
i++;
}
}
}
15
·Best, worst, and average time complexity of algorithm express what the
resource usage is at least, at most and on average, respectively.
·In merge Sort algorithm it can be expressed as,
T(n)=T(n/2)+T(n/2)+ ɵ(n)
T(n) = 2T(n/2) + ɵ(n)
T(n) ={2T(n/2) + cn} (if n>1)
f(n)=cn
T(n)= ɵ(nlgn)
·In all 3 cases (worst, average and best) time complexity of Merge Sort
is ɵ(nLogn).
Time Complexity
16
Space Complexity
● Space complexity of merge sort is O(n).
● In every recursive call that we create an array for
merging and they take no more than O(n)space.
● When the merging is done ,these arrays are
deleted and some new ones will be created in
some other recursive call.
17
Comparison with other sort algorithms
•Merge sort is a stable sort and is more efficient at handling slow to
access sequential media
•Merge sort is often best choice for sorting a linked list
•Heapsort has same time bounds as merge sort (heapsort requires
only O(1) and merge sort requires O(n) )
•Relatively easy to implement merge sort in such a way it requires
only O(1) extra space
•Slow random access performance of a linked list make some other
algorithms perform poor (quick sort) and some others completely
impossible (heapsort)
18
Thank You!
19

More Related Content

PPT
Data Structure and Algorithms Heaps and Trees
PPTX
Merge sort
PPTX
Array sorting
PPT
Selection sort
PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
PPTX
Merge sort algorithm power point presentation
PPTX
Presentation-Merge Sort
Data Structure and Algorithms Heaps and Trees
Merge sort
Array sorting
Selection sort
Analysis of Algorithm (Bubblesort and Quicksort)
Merge sort algorithm power point presentation
Presentation-Merge Sort

What's hot (20)

PPT
PPT
Mergesort
PPTX
Counting Sort
PPTX
Doubly Linked List
PPTX
Stressen's matrix multiplication
PPT
Merge sort
PPTX
Kruskal Algorithm
PPTX
Sparse matrix and its representation data structure
PPTX
Fractional Knapsack Problem
PPTX
Linear data structure concepts
PPT
Quick Sort
PPTX
PPTX
Divide and conquer - Quick sort
PPTX
Insertion sort
PPTX
Data Structure and Algorithms Merge Sort
PPTX
Binary search
PPTX
Linked list
PPTX
Merge sort
PPTX
Marge Sort
PPTX
Quick sort
Mergesort
Counting Sort
Doubly Linked List
Stressen's matrix multiplication
Merge sort
Kruskal Algorithm
Sparse matrix and its representation data structure
Fractional Knapsack Problem
Linear data structure concepts
Quick Sort
Divide and conquer - Quick sort
Insertion sort
Data Structure and Algorithms Merge Sort
Binary search
Linked list
Merge sort
Marge Sort
Quick sort
Ad

Similar to Merge sort (20)

PDF
advanced algo
PPTX
dsa presentation on merge sorting in C++.pptx
PPTX
Data structure Merge Sort implementation
PPTX
DSA- Merge Sort-a sorting technique.pptx
PPTX
Merge sort analysis and its real time applications
PPTX
presentation.pptx
PPTX
merg sort slide created by anas sanan.pptx
PPTX
MergeSort presentation dgdfgdfgdfgg.pptx
PPTX
Presentation merge sort.pptx
PPTX
Merge sort
PPTX
Data Structure Marge sort Group 5 pptx so
PPTX
Merge sort
PPTX
Merge Sort with real example using animation
PPT
Merge sort
PPTX
Merge sort
PPTX
24671A08972ds.pptxxxxxxxxxkkkskjsjsjskskssjdhdjskw
PPTX
Lecture -16-merge sort (slides).pptx
PDF
Working of Merge Sort Code
PPTX
Merge Sort (w/ principle, algorithm, code, visualizations)
advanced algo
dsa presentation on merge sorting in C++.pptx
Data structure Merge Sort implementation
DSA- Merge Sort-a sorting technique.pptx
Merge sort analysis and its real time applications
presentation.pptx
merg sort slide created by anas sanan.pptx
MergeSort presentation dgdfgdfgdfgg.pptx
Presentation merge sort.pptx
Merge sort
Data Structure Marge sort Group 5 pptx so
Merge sort
Merge Sort with real example using animation
Merge sort
Merge sort
24671A08972ds.pptxxxxxxxxxkkkskjsjsjskskssjdhdjskw
Lecture -16-merge sort (slides).pptx
Working of Merge Sort Code
Merge Sort (w/ principle, algorithm, code, visualizations)
Ad

Recently uploaded (20)

PDF
Pre independence Education in Inndia.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
master seminar digital applications in india
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Sports Quiz easy sports quiz sports quiz
PDF
RMMM.pdf make it easy to upload and study
PPTX
Lesson notes of climatology university.
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
01-Introduction-to-Information-Management.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pre independence Education in Inndia.pdf
Insiders guide to clinical Medicine.pdf
Microbial diseases, their pathogenesis and prophylaxis
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
master seminar digital applications in india
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Sports Quiz easy sports quiz sports quiz
RMMM.pdf make it easy to upload and study
Lesson notes of climatology university.
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
GDM (1) (1).pptx small presentation for students
Supply Chain Operations Speaking Notes -ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
VCE English Exam - Section C Student Revision Booklet
O7-L3 Supply Chain Operations - ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

Merge sort

  • 1. Merge Sort Data Structures and Algorithms
  • 2. Content ● What is Merge Sort? ● How it happens? ● Algorithm ● Implementation ● Complexity ● Comparison with other sort algorithms ● Advantages and Disadvantages
  • 3. What is Merge Sort? 3 What is merging? ● Mixing two array elements in ascending order to produce a third array in ascending. Ex: 12 15 45 10 22 30 10 12 15 22 30 45 Array A Array B Array C
  • 4. » Merge sort is a sorting technique based on divide and conquer technique with worst-case time complexity being O(n log n) » In computer science, merge sort is an efficient, most respected ,general-purpose and comparison-based sorting algorithm » In simply, merge sort first divides the array into equal halves and then combines them in a sorted manner 4
  • 5. 5 » To merge two sorted arrays,firstly we index both arrays starting at zero,where the smallest element is located. » Comparing the elements at each index,we choose the smaller element,put it into the array that we are merging into. » Increment the index of the smaller element. » By this method,we continually select the next smallest element from the two arrays and merge them into sorted array.
  • 6. How it happens? Divide Problem is decomposed into one or more sub problems Conquer Every sub problem is solved recursively Combine Merge the solutions of subproblems to create the solution of the original problem 6
  • 7. How it happens cont; Ex: Array Name : A Starting index : p Ending index : r Array : A[p..r] 7
  • 8. How it happens cont; Divide Mid point of p and r : q the subarrays : A[p..q] and A[q+1, r] Conquer Sorting subarrays A[p..q] and A[q+1, r] till the base case is reached Combine Combining two sorted subarrays A[p..q] and A[q+1, r] 8
  • 9. 9 Algorithm MergeSort(l,h){ if(l<h){ mid=(l+h)/2; MergeSort(l,mid); MergeSort((mid+1),h); Merge(l,mid,h); } } 4 1 3 47 0 1 2 43 Low Mid High l = low , h = high 2 5 6 0 1 2 3 8 5 76 9 Algorithm
  • 10. 10 4 1 3 4 2 5 6 0 1 2 3 8 5 76 7 0,7 0,3 4,7 0,1 2,3 4,5 6,7 0,0 1,1 2,2 3,3 4,4 5,5 6,6 7,7 Split Merge 1 2 3 4 5 6 7 merging steps
  • 11. public class MergeSortEasy { private static void mergesort(int low, int high){ if(low < high){ int middle = (high + low) / 2; mergesort(low, middle); mergesort(middle + 1, high); merge(low, middle, high); } } 11 Implementation
  • 12. 12 private static void merge(int low, int middle, int high){ for(int i = low; i <= high; i++){ helper[i] = numbers[i]; } int i = low; int j = middle + 1; int k = low;
  • 13. 13 while(i <= middle && j <= high){ if(helper[i] <= helper[j]){ numbers[k] = helper[i]; i++; } else { numbers[k] = helper[j]; j++; } k++; }
  • 14. 14 while( i <= middle){ numbers[k] = helper[i]; k++; i++; } } }
  • 15. 15
  • 16. ·Best, worst, and average time complexity of algorithm express what the resource usage is at least, at most and on average, respectively. ·In merge Sort algorithm it can be expressed as, T(n)=T(n/2)+T(n/2)+ ɵ(n) T(n) = 2T(n/2) + ɵ(n) T(n) ={2T(n/2) + cn} (if n>1) f(n)=cn T(n)= ɵ(nlgn) ·In all 3 cases (worst, average and best) time complexity of Merge Sort is ɵ(nLogn). Time Complexity 16
  • 17. Space Complexity ● Space complexity of merge sort is O(n). ● In every recursive call that we create an array for merging and they take no more than O(n)space. ● When the merging is done ,these arrays are deleted and some new ones will be created in some other recursive call. 17
  • 18. Comparison with other sort algorithms •Merge sort is a stable sort and is more efficient at handling slow to access sequential media •Merge sort is often best choice for sorting a linked list •Heapsort has same time bounds as merge sort (heapsort requires only O(1) and merge sort requires O(n) ) •Relatively easy to implement merge sort in such a way it requires only O(1) extra space •Slow random access performance of a linked list make some other algorithms perform poor (quick sort) and some others completely impossible (heapsort) 18

Editor's Notes

  • #12: *Divide and conquer *Recurrion is taken significant place *what is recursion
  • #13: * Adding values on temporary on a data structure
  • #14: *Choose the smallest and highest value *0th and 1th
  • #15: *shift the values