SlideShare a Scribd company logo
1 of 17
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

What's hot

What's hot (20)

Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Linklist
LinklistLinklist
Linklist
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Linked list
Linked listLinked list
Linked list
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms II
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 

Similar to Merge sort

Please I want a detailed complete answers for each part.Then make.pdf
Please I want a detailed complete answers for each part.Then make.pdfPlease I want a detailed complete answers for each part.Then make.pdf
Please I want a detailed complete answers for each part.Then make.pdfsiennatimbok52331
 
14-sorting (3).ppt
14-sorting (3).ppt14-sorting (3).ppt
14-sorting (3).pptyasser3omr
 
Using Excel Functions
Using Excel FunctionsUsing Excel Functions
Using Excel FunctionsGautam Gupta
 
it skill excel sheet for ppt.pptx
it skill excel sheet for ppt.pptxit skill excel sheet for ppt.pptx
it skill excel sheet for ppt.pptxMdAquibRazi1
 
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1Taisuke Oe
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applicationsyazad dumasia
 
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
 
Basic operators in matlab
Basic operators in matlabBasic operators in matlab
Basic operators in matlabrishiteta
 
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
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfsaneshgamerz
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfNiravPanchal50
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptxchin463670
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureNurjahan Nipa
 

Similar to Merge sort (20)

Please I want a detailed complete answers for each part.Then make.pdf
Please I want a detailed complete answers for each part.Then make.pdfPlease I want a detailed complete answers for each part.Then make.pdf
Please I want a detailed complete answers for each part.Then make.pdf
 
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
 
Using Excel Functions
Using Excel FunctionsUsing Excel Functions
Using Excel Functions
 
it skill excel sheet for ppt.pptx
it skill excel sheet for ppt.pptxit skill excel sheet for ppt.pptx
it skill excel sheet for ppt.pptx
 
How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1How to start functional programming (in Scala): Day1
How to start functional programming (in Scala): Day1
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
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
 
Basic operators in matlab
Basic operators in matlabBasic operators in matlab
Basic operators in matlab
 
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...
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
CP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdfCP PPT_Unit IV computer programming in c.pdf
CP PPT_Unit IV computer programming in c.pdf
 
Java Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdfJava Algorithm Interview Questions & Answers .pdf
Java Algorithm Interview Questions & Answers .pdf
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 

More from Md. Rakib Trofder

Software System Reconstruction using large language model
Software System Reconstruction using large language modelSoftware System Reconstruction using large language model
Software System Reconstruction using large language modelMd. Rakib Trofder
 
Daily Scrum, Sprint Review & Retrospective.pptx
Daily Scrum, Sprint Review & Retrospective.pptxDaily Scrum, Sprint Review & Retrospective.pptx
Daily Scrum, Sprint Review & Retrospective.pptxMd. Rakib Trofder
 
Scrum & Sprint Planning.pptx
Scrum & Sprint Planning.pptxScrum & Sprint Planning.pptx
Scrum & Sprint Planning.pptxMd. Rakib Trofder
 
Agricultural Business with Technology
Agricultural Business with TechnologyAgricultural Business with Technology
Agricultural Business with TechnologyMd. Rakib Trofder
 
Twitter Timeline and Search Distributed System.pptx
Twitter Timeline and Search Distributed System.pptxTwitter Timeline and Search Distributed System.pptx
Twitter Timeline and Search Distributed System.pptxMd. Rakib Trofder
 
Artificial Intelligence in Gaming.pptx
Artificial Intelligence in Gaming.pptxArtificial Intelligence in Gaming.pptx
Artificial Intelligence in Gaming.pptxMd. Rakib Trofder
 
Mechanism behind BlogBee Application
Mechanism behind BlogBee ApplicationMechanism behind BlogBee Application
Mechanism behind BlogBee ApplicationMd. Rakib Trofder
 
Massive Open Online Courses (MOOC)
Massive Open Online Courses (MOOC)Massive Open Online Courses (MOOC)
Massive Open Online Courses (MOOC)Md. Rakib Trofder
 
BlogBee A Blog Based Social Media.pptx
BlogBee A Blog Based Social Media.pptxBlogBee A Blog Based Social Media.pptx
BlogBee A Blog Based Social Media.pptxMd. Rakib Trofder
 
INTER-SYSTEMS EARNS ISO 9001-2008 CERTIFICATION
INTER-SYSTEMS EARNS ISO 9001-2008 CERTIFICATIONINTER-SYSTEMS EARNS ISO 9001-2008 CERTIFICATION
INTER-SYSTEMS EARNS ISO 9001-2008 CERTIFICATIONMd. Rakib Trofder
 
Web Technology Tag Presentation.pptx
Web Technology Tag Presentation.pptxWeb Technology Tag Presentation.pptx
Web Technology Tag Presentation.pptxMd. Rakib Trofder
 
Video to text blog (blog bee)
Video to text blog (blog bee)Video to text blog (blog bee)
Video to text blog (blog bee)Md. Rakib Trofder
 
Http status code 416 vs 428, 503 vs 505
Http status code  416 vs 428, 503 vs 505Http status code  416 vs 428, 503 vs 505
Http status code 416 vs 428, 503 vs 505Md. Rakib Trofder
 

More from Md. Rakib Trofder (20)

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

Recently uploaded

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

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