SlideShare a Scribd company logo
Merge sort
Created by : Rakib & Tahmeed
1
Created & Edited By:
Md.
Rakib
Trofder
•BSSE1129
Tahmeed
Mahbub
•BSSE1127
2
What is Merge Sort
Merge Sort is one kind of sorting
that based on Divide and
Conquer algorithm .
3
How it works
•
It divides input array in two
halves , calls itself (using
recursive function) for the
two halves and then merges
the two sorted halves .
4
Merge sort Simulation
Unsorted element 
56 32 16 23 9 17 3 21
5
Dividing the array
56 32 16 23 9 17 3 21
56 32 16 23 9 17 3 21
56 32 16 23 9 17 3 21
56 32 16 23 9 17 3 21
6
Merging the array
56 32 16 23 9 17 3 21
5632 16 23 9 17 3 21
563216 23 9 173 21
563216 239 173 21
7
Sorted element 
563216 239 173 21
8
Analysis of Sorting
Main
function
Merge_sort
function
Merging
Function
9
Code of Main Function
#include<stdio.h>
#include<stdlib.h>
void merge_sort(int , int);
void merging(int ,int ,int);
int a[] ={56,32,16,23,9,17,3,21};
int main()
{
int i,n=8;
merge_sort(0,n);
for(i=0;i<=n; i++)
{
printf("%d ",a[i]);
}
return 0;
}
10
Code of Merge_Sort
void merge_sort(int left , int right)
{
if(left>=right)
{
return ;
}
int mid= left + (right-left)/2;
merge_sort( left, mid);
merge_sort( mid+1, right) ;
merging( left, mid, right );
}
11
Code of Merging Function (part-1)
void merging(int left,int mid,int right)
{
int i;
int k, left_index =0, right_index =0;
int L_size, R_size;
int p,q;
L_size = mid-left+1;
R_size = right - mid;
p=L_size;
q=R_size;
int L[p], R[q];
for(i=0; i<p; i++)
{
L[i]=a[left+i];
}
for(i=0; i<q; i++)
{
R[i]=a[mid+1+i];
}
12
Code of Merging Function (part-2)
for(k=left; left_index<p&&right_index<q;
k++)
{
if(L[left_index]<R[right_index])
{
a[k]=L[left_index];
left_index++;
}
else
{
a[k]=R[right_index];
right_index++;
}
}
13
Code of Merging Function (part-3)
while(left_index<L_size)
{
a[k]=L[left_index];
left_index++;
k++;
}
while(right_index<L_size)
{
a[k]=R[right_index];
right_index++;
k++;
}
}
14
Time Complexity of merge sort
Complexity of merge sort is :
Because At the time of dividing we separate them into two halves .
So the complexity of this part is O(log n) .
Then we merge all the element on increasing or decreasing order. So
in this step the complexity of merging is O(n).
So the overall time complexity of merge sort is O(n * log (n) ).
O(n * log(n))
15
Effeciency of Merge Sort
The time complexity of bubble sort , selection sort , insertion sort is
equal to O(n*n).
But the time complexity of merge sort is equal to O(n * log n).
So we need less time for sorting by merge than other sort. So this is
very efficient for computer organization as well as algorithm &
data structure .
16
17

More Related Content

PPT
Selection sort
PPTX
Searching & Sorting Algorithms
PDF
Sorting Algorithms
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
Insertion sort
PPTX
Quick sort-Data Structure
PPTX
Bubble sort
PPTX
Sorting algorithms
Selection sort
Searching & Sorting Algorithms
Sorting Algorithms
Binary Search - Design & Analysis of Algorithms
Insertion sort
Quick sort-Data Structure
Bubble sort
Sorting algorithms

What's hot (20)

PPT
3.2 insertion sort
PPTX
Searching
PPTX
Different Sorting tecniques in Data Structure
PDF
Quick Sort , Merge Sort , Heap Sort
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PDF
Searching and Sorting Techniques in Data Structure
PPT
Linked List
PPTX
Searching and sorting
PPT
Searching algorithm
PPTX
Insertion sort
PPTX
Insertion sort
PPT
Merge sort
PPTX
Bubble sort | Data structure |
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPTX
Merge sort and quick sort
PPTX
Quick sort
PPTX
Stack Data Structure
PPTX
Insertion Sort
PPTX
Selection sort
3.2 insertion sort
Searching
Different Sorting tecniques in Data Structure
Quick Sort , Merge Sort , Heap Sort
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Searching and Sorting Techniques in Data Structure
Linked List
Searching and sorting
Searching algorithm
Insertion sort
Insertion sort
Merge sort
Bubble sort | Data structure |
Data Structures - Lecture 8 [Sorting Algorithms]
Merge sort and quick sort
Quick sort
Stack Data Structure
Insertion Sort
Selection sort
Ad

Similar to Merge sort (20)

PPTX
dsa presentation on merge sorting in C++.pptx
PDF
Please I want a detailed complete answers for each part.Then make.pdf
DOCX
object oriented programming lab manual .docx
PPT
14-sorting.ppt
PPT
14-sorting (3).ppt
PPT
14-sorting.ppt
PPT
14-sorting.ppt
PPTX
Using Excel Functions
PPTX
it skill excel sheet for ppt.pptx
PDF
How to start functional programming (in Scala): Day1
PPTX
Introduction to Algorithms
PPTX
Merge sort analysis and its real time applications
PDF
IRJET- A Survey on Different Searching Algorithms
PPTX
Basic operators in matlab
PDF
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
DOCX
cs3381-object oriented programming-ab-manual
PPT
Functional Programming
PDF
CP PPT_Unit IV computer programming in c.pdf
PDF
Java Algorithm Interview Questions & Answers .pdf
PPT
16-sorting.ppt
dsa presentation on merge sorting in C++.pptx
Please I want a detailed complete answers for each part.Then make.pdf
object oriented programming lab manual .docx
14-sorting.ppt
14-sorting (3).ppt
14-sorting.ppt
14-sorting.ppt
Using Excel Functions
it skill excel sheet for ppt.pptx
How to start functional programming (in Scala): Day1
Introduction to Algorithms
Merge sort analysis and its real time applications
IRJET- A Survey on Different Searching Algorithms
Basic operators in matlab
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
cs3381-object oriented programming-ab-manual
Functional Programming
CP PPT_Unit IV computer programming in c.pdf
Java Algorithm Interview Questions & Answers .pdf
16-sorting.ppt
Ad

More from Md. Rakib Trofder (20)

PPTX
Software System Reconstruction using large language model
PPTX
Daily Scrum, Sprint Review & Retrospective.pptx
PPTX
Scrum & Sprint Planning.pptx
PPTX
Agricultural Business with Technology
PPTX
HTTP Caching.pptx
PPTX
Twitter Timeline and Search Distributed System.pptx
PPTX
Artificial Intelligence in Gaming.pptx
PPTX
Mechanism behind BlogBee Application
PPTX
Massive Open Online Courses (MOOC)
PPTX
Design Pattern.pptx
PPTX
BlogBee A Blog Based Social Media.pptx
PPTX
INTER-SYSTEMS EARNS ISO 9001-2008 CERTIFICATION
PPTX
Web Technology Tag Presentation.pptx
PPTX
Video to text blog (blog bee)
PPTX
Web tech tag explanation
PPTX
Library assistant tool
PPTX
Http status code 416 vs 428, 503 vs 505
PPTX
Artificial Intelligence
PPTX
Page rank algorithm
PPTX
Analytic hierarchy process
Software System Reconstruction using large language model
Daily Scrum, Sprint Review & Retrospective.pptx
Scrum & Sprint Planning.pptx
Agricultural Business with Technology
HTTP Caching.pptx
Twitter Timeline and Search Distributed System.pptx
Artificial Intelligence in Gaming.pptx
Mechanism behind BlogBee Application
Massive Open Online Courses (MOOC)
Design Pattern.pptx
BlogBee A Blog Based Social Media.pptx
INTER-SYSTEMS EARNS ISO 9001-2008 CERTIFICATION
Web Technology Tag Presentation.pptx
Video to text blog (blog bee)
Web tech tag explanation
Library assistant tool
Http status code 416 vs 428, 503 vs 505
Artificial Intelligence
Page rank algorithm
Analytic hierarchy process

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Big Data Technologies - Introduction.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Electronic commerce courselecture one. Pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Dropbox Q2 2025 Financial Results & Investor Presentation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Machine learning based COVID-19 study performance prediction
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Diabetes mellitus diagnosis method based random forest with bat algorithm
Big Data Technologies - Introduction.pptx
Review of recent advances in non-invasive hemoglobin estimation
MYSQL Presentation for SQL database connectivity
sap open course for s4hana steps from ECC to s4
Reach Out and Touch Someone: Haptics and Empathic Computing
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation theory and applications.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Digital-Transformation-Roadmap-for-Companies.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Merge sort

  • 1. Merge sort Created by : Rakib & Tahmeed 1
  • 2. Created & Edited By: Md. Rakib Trofder •BSSE1129 Tahmeed Mahbub •BSSE1127 2
  • 3. What is Merge Sort Merge Sort is one kind of sorting that based on Divide and Conquer algorithm . 3
  • 4. How it works • It divides input array in two halves , calls itself (using recursive function) for the two halves and then merges the two sorted halves . 4
  • 5. Merge sort Simulation Unsorted element  56 32 16 23 9 17 3 21 5
  • 6. Dividing the array 56 32 16 23 9 17 3 21 56 32 16 23 9 17 3 21 56 32 16 23 9 17 3 21 56 32 16 23 9 17 3 21 6
  • 7. Merging the array 56 32 16 23 9 17 3 21 5632 16 23 9 17 3 21 563216 23 9 173 21 563216 239 173 21 7
  • 10. Code of Main Function #include<stdio.h> #include<stdlib.h> void merge_sort(int , int); void merging(int ,int ,int); int a[] ={56,32,16,23,9,17,3,21}; int main() { int i,n=8; merge_sort(0,n); for(i=0;i<=n; i++) { printf("%d ",a[i]); } return 0; } 10
  • 11. Code of Merge_Sort void merge_sort(int left , int right) { if(left>=right) { return ; } int mid= left + (right-left)/2; merge_sort( left, mid); merge_sort( mid+1, right) ; merging( left, mid, right ); } 11
  • 12. Code of Merging Function (part-1) void merging(int left,int mid,int right) { int i; int k, left_index =0, right_index =0; int L_size, R_size; int p,q; L_size = mid-left+1; R_size = right - mid; p=L_size; q=R_size; int L[p], R[q]; for(i=0; i<p; i++) { L[i]=a[left+i]; } for(i=0; i<q; i++) { R[i]=a[mid+1+i]; } 12
  • 13. Code of Merging Function (part-2) for(k=left; left_index<p&&right_index<q; k++) { if(L[left_index]<R[right_index]) { a[k]=L[left_index]; left_index++; } else { a[k]=R[right_index]; right_index++; } } 13
  • 14. Code of Merging Function (part-3) while(left_index<L_size) { a[k]=L[left_index]; left_index++; k++; } while(right_index<L_size) { a[k]=R[right_index]; right_index++; k++; } } 14
  • 15. Time Complexity of merge sort Complexity of merge sort is : Because At the time of dividing we separate them into two halves . So the complexity of this part is O(log n) . Then we merge all the element on increasing or decreasing order. So in this step the complexity of merging is O(n). So the overall time complexity of merge sort is O(n * log (n) ). O(n * log(n)) 15
  • 16. Effeciency of Merge Sort The time complexity of bubble sort , selection sort , insertion sort is equal to O(n*n). But the time complexity of merge sort is equal to O(n * log n). So we need less time for sorting by merge than other sort. So this is very efficient for computer organization as well as algorithm & data structure . 16
  • 17. 17