SlideShare une entreprise Scribd logo
1  sur  12
MERGE SORT  UNDERSTANDING CODE in C By-MohitTare       mohittare@yahoo.com
MERGE  SORT – understanding code #include<stdio.h> intmergesort(int*,int,int); void merge(int*,int,int,int); int main() { int a[5]={3,5,2,6,8}; inti; mergesort(a,0,4); 	for(i=0;i<5;i++) printf("%d",a[i]); 	return 0; } 3    5    2     6    8
intmergesort(inta[], int low, int high) {			0,4 int mid;			 if(low<high) {     mid=(low+high)/2;(2) mergesort(a,low,mid);(a,0,2) mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0); } intmergesort(int a[], int low, int high) {			0,0 int mid; if(low<high)(0<0-false) { mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0);(now return will give control to Nearest function call) } intmergesort(int a[], int low, int high) {			0,2 int mid; if(low<high) { mid=(low+high)/2;(1) mergesort(a,low,mid);(0,1) mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0); } intmergesort(int a[], int low, int high) {			0,1 int mid; if(low<high) { mid=(low+high)/2;(0) mergesort(a,low,mid);(0,0) mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0); }
intmergesort(inta[], int low, int high) {			0,1 int mid; if(low<high) {     mid=(low+high)/2;(0) mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); }		0,1,0 return(0); } intmergesort(int a[], int low, int high) {			0,0 int mid; if(low<high)(0<0-false) { mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0); } intmergesort(int a[], int low, int high) {			(1,1) int mid; if(low<high)(1<1-false) { mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0); } intmergesort(int a[], int low, int high) {			0,1 int mid; if(low<high) { mid=(low+high)/2;(0) mergesort(a,low,mid);(0,0) mergesort(a,mid+1,high);(0+1,1) merge(a,low,high,mid); } return(0); }
Until now array has been divided into 1 element each. Now array is merged and sorted in the merge function
void merge(int a[], int low, int high, int mid) {                                 0,1,0 int b[20],l1,r1,i; l1=low;0										 r1=mid+1;1					      l1       r1                                          i i=low;0					 while((l1<=mid) && (r1<=high)) {          0<=0                      1<=1 	if(a[l1]<a[r1]) 	{    3<5 		b[i]=a[l1];b[0]=3 		l1=l1+1;l1=1 i=i+1;i1=1 	} 	else  	{ 		b[i]=a[r1]; 		r1=r1+1; i=i+1; 	} } while(l1<=mid)1<=0 { 	b[i]=a[l1]; 	l1=l1+1; i=i+1; } while(r1<=high)1<=1 { 	b[i]=a[r1];b[1]=5 	r1=r1+1;r1=2 i=i+1;i=2 } For(j=low;j<i;j++) 	a[j]=b[j]; } 3                	  3    5    2     6    8	  1. 3    5            	  3    5    2     6    8	  i 3    5
|----------------------; |----------------------; while(r1<=high) { 	b[i]=a[r1]; 	r1=r1+1; i=i+1; } It will transfer the  current values  of   b array to  a array for(j=low;j<i ;j++) { 	a[j]=b[j]; } }
IN  next recursive pass or call to merge  3    5            	  3    5    2     6    8	  void merge(int a[], int low, int high, int mid) {	            0,2,1 int b[20],l1,r1,i; l1=low;0 r1=mid+1;2                                                                                             l1             r1               i=low;0 while((l1<=mid) && (r1<=high)) {          0<=1                    2<=2 	if(a[l1]<a[r1]) 	{    3<2 		b[i]=a[l1]; 		l1=l1+1;                                                      l1 i=i+1; 	} 	else  	{ 		b[i]=a[r1];b[0]=2 		r1=r1+1;r1=3 i=i+1;i=1 	} } while(l1<=mid)0<=1 { 	b[i]=a[l1];b1=3         	l1=l1+1;l1=1             i=i+1;i=2                    } while(r1<=high)3<=2 { 	b[i]=a[r1]; 	r1=r1+1; i=i+1; } for(j=low;j<i;j++) 	a[j]=b[j]; } 3    5            	  3    5    2     6    8	  3    5    2     6    8	  2    5            	  r1 3    5    2     6    8	  2    3            	  3    5    2     6    8	  2    3     5
IN  next recursive pass or call to merge  2    3     5       	  2    3    5     6    8	  void merge(int a[], int low, int high, int mid) {		a,3,4,3 int b[20],l1,r1,i; l1=low;l1=3 r1=mid+1;r1=4 i=low;i=3 while((l1<=mid) && (r1<=high)) {          3<=3                 4<=4 	if(a[l1]<a[r1]) 	{ 		b[i]=a[l1];b[3]=6 		l1=l1+1;l1=4 i=i+1;i1=4 	} 	else  	{ 		b[i]=a[r1]; 		r1=r1+1; i=i+1; 	} } while(l1<=mid) { 	b[i]=a[l1]; 	l1=l1+1; i=i+1; } while(r1<=high)4<=4 { 	b[i]=a[r1];b[4]=8 	r1=r1+1;r1=5 i=i+1;i=5 } For(j=low;j<i;j++) 	a[j]=b[j]; } 2    3    5    6    8	  2    3     5       	  2    3     5     6  	  2    3    5     6    8	  2    3     5    6    8
IN  LAST  recursive pass or call to merge  2    3     5    6    8	  2    3    5     6    8	  void merge(int a[], int low, int high, int mid) {	           0,4,2 int b[20],l1,r1,i; l1=low;l1=0 r1=mid+1;r1=3 i=low;i=0 while((l1<=mid) && (r1<=high)) {             0<=2              3<=4 	if(a[l1]<a[r1]) 	{     2<6 		b[i]=a[l1];b[0]=2 		l1=l1+1;l1=1 i=i+1;i=1 	} 	else  	{ 		b[i]=a[r1]; 		r1=r1+1; i=i+1; 	} } while(l1<=mid) { 	b[i]=a[l1]; 	l1=l1+1; i=i+1; } while(r1<=high)3<=4 { 	b[i]=a[r1];b[3]=6 	r1=r1+1;r1=4 i=i+1;i=4 } For(j=low;j<i;j++ 	a[j]=b[j]; } 2    3    5    6    8	  2    3     5    6    8	  2    3    5    6    8	  2    3     5    6    8	  2    3    5    6    8	  2    3     5    6    8	  2    3    5    6    8	  2    3     5    6    8	  2    3    5    6    8	  2    3     5    6    8
FINALLY SORTED ARRAY 2    3    5    6    8
THANK YOU!

Contenu connexe

Tendances

Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihmSajid Marwat
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problemsSumita Das
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sortKrish_ver2
 
Hash table
Hash tableHash table
Hash tableVu Tran
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithnKumar
 
INTRODUCTION TO ALGORITHMS Third Edition
INTRODUCTION TO ALGORITHMS Third EditionINTRODUCTION TO ALGORITHMS Third Edition
INTRODUCTION TO ALGORITHMS Third EditionPHI Learning Pvt. Ltd.
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary searchNisha Soms
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEMi i
 

Tendances (20)

Time complexity
Time complexityTime complexity
Time complexity
 
Mergesort
MergesortMergesort
Mergesort
 
Presentation-Merge Sort
Presentation-Merge SortPresentation-Merge Sort
Presentation-Merge Sort
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Big O Notation
Big O NotationBig O Notation
Big O Notation
 
Data structure by Digvijay
Data structure by DigvijayData structure by Digvijay
Data structure by Digvijay
 
Merge sort
Merge sortMerge sort
Merge sort
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stack
StackStack
Stack
 
3.2 insertion sort
3.2 insertion sort3.2 insertion sort
3.2 insertion sort
 
Hash table
Hash tableHash table
Hash table
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Quick sort algorithn
Quick sort algorithnQuick sort algorithn
Quick sort algorithn
 
INTRODUCTION TO ALGORITHMS Third Edition
INTRODUCTION TO ALGORITHMS Third EditionINTRODUCTION TO ALGORITHMS Third Edition
INTRODUCTION TO ALGORITHMS Third Edition
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort
Merge sortMerge sort
Merge sort
 

En vedette

En vedette (20)

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
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
 
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
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Shell sort in Data Structure Using C
Shell sort in Data Structure Using CShell sort in Data Structure Using C
Shell sort in Data Structure Using C
 
Shell sort[1]
Shell sort[1]Shell sort[1]
Shell sort[1]
 
Insertion and merge sort
Insertion and merge sortInsertion and merge sort
Insertion and merge sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Shellsort
ShellsortShellsort
Shellsort
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)
 
05 dc1
05 dc105 dc1
05 dc1
 
Shell sort slide
Shell sort slideShell sort slide
Shell sort slide
 
Shell sort
Shell sortShell sort
Shell sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Shell sort
Shell sortShell sort
Shell sort
 

Similaire à Understanding Merge Sort Algorithm in C

Similaire à Understanding Merge Sort Algorithm in C (20)

11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Sorting techniques
Sorting techniques Sorting techniques
Sorting techniques
 
Home workassignment01 google docs
Home workassignment01   google docsHome workassignment01   google docs
Home workassignment01 google docs
 
c++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdfc++ program I need to sort arrays using an insertion sort and a mer.pdf
c++ program I need to sort arrays using an insertion sort and a mer.pdf
 
Pointer
PointerPointer
Pointer
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Safe int
Safe intSafe int
Safe int
 
Vcs16
Vcs16Vcs16
Vcs16
 
DAA Lab Work.docx
DAA Lab Work.docxDAA Lab Work.docx
DAA Lab Work.docx
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
Java operators
Java operatorsJava operators
Java operators
 
Pj01 4-operators and control flow
Pj01 4-operators and control flowPj01 4-operators and control flow
Pj01 4-operators and control flow
 
Ds program-print
Ds program-printDs program-print
Ds program-print
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
Help with root locus homework1
Help with root locus homework1Help with root locus homework1
Help with root locus homework1
 
pointers 1
pointers 1pointers 1
pointers 1
 
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
 
C programs
C programsC programs
C programs
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 

Dernier

Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Dernier (20)

Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Understanding Merge Sort Algorithm in C

  • 1. MERGE SORT UNDERSTANDING CODE in C By-MohitTare mohittare@yahoo.com
  • 2. MERGE SORT – understanding code #include<stdio.h> intmergesort(int*,int,int); void merge(int*,int,int,int); int main() { int a[5]={3,5,2,6,8}; inti; mergesort(a,0,4); for(i=0;i<5;i++) printf("%d",a[i]); return 0; } 3 5 2 6 8
  • 3. intmergesort(inta[], int low, int high) { 0,4 int mid; if(low<high) { mid=(low+high)/2;(2) mergesort(a,low,mid);(a,0,2) mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0); } intmergesort(int a[], int low, int high) { 0,0 int mid; if(low<high)(0<0-false) { mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0);(now return will give control to Nearest function call) } intmergesort(int a[], int low, int high) { 0,2 int mid; if(low<high) { mid=(low+high)/2;(1) mergesort(a,low,mid);(0,1) mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0); } intmergesort(int a[], int low, int high) { 0,1 int mid; if(low<high) { mid=(low+high)/2;(0) mergesort(a,low,mid);(0,0) mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0); }
  • 4. intmergesort(inta[], int low, int high) { 0,1 int mid; if(low<high) { mid=(low+high)/2;(0) mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); } 0,1,0 return(0); } intmergesort(int a[], int low, int high) { 0,0 int mid; if(low<high)(0<0-false) { mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0); } intmergesort(int a[], int low, int high) { (1,1) int mid; if(low<high)(1<1-false) { mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); } return(0); } intmergesort(int a[], int low, int high) { 0,1 int mid; if(low<high) { mid=(low+high)/2;(0) mergesort(a,low,mid);(0,0) mergesort(a,mid+1,high);(0+1,1) merge(a,low,high,mid); } return(0); }
  • 5. Until now array has been divided into 1 element each. Now array is merged and sorted in the merge function
  • 6. void merge(int a[], int low, int high, int mid) { 0,1,0 int b[20],l1,r1,i; l1=low;0 r1=mid+1;1 l1 r1 i i=low;0 while((l1<=mid) && (r1<=high)) { 0<=0 1<=1 if(a[l1]<a[r1]) { 3<5 b[i]=a[l1];b[0]=3 l1=l1+1;l1=1 i=i+1;i1=1 } else { b[i]=a[r1]; r1=r1+1; i=i+1; } } while(l1<=mid)1<=0 { b[i]=a[l1]; l1=l1+1; i=i+1; } while(r1<=high)1<=1 { b[i]=a[r1];b[1]=5 r1=r1+1;r1=2 i=i+1;i=2 } For(j=low;j<i;j++) a[j]=b[j]; } 3 3 5 2 6 8 1. 3 5 3 5 2 6 8 i 3 5
  • 7. |----------------------; |----------------------; while(r1<=high) { b[i]=a[r1]; r1=r1+1; i=i+1; } It will transfer the current values of b array to a array for(j=low;j<i ;j++) { a[j]=b[j]; } }
  • 8. IN next recursive pass or call to merge 3 5 3 5 2 6 8 void merge(int a[], int low, int high, int mid) { 0,2,1 int b[20],l1,r1,i; l1=low;0 r1=mid+1;2 l1 r1 i=low;0 while((l1<=mid) && (r1<=high)) { 0<=1 2<=2 if(a[l1]<a[r1]) { 3<2 b[i]=a[l1]; l1=l1+1; l1 i=i+1; } else { b[i]=a[r1];b[0]=2 r1=r1+1;r1=3 i=i+1;i=1 } } while(l1<=mid)0<=1 { b[i]=a[l1];b1=3 l1=l1+1;l1=1 i=i+1;i=2 } while(r1<=high)3<=2 { b[i]=a[r1]; r1=r1+1; i=i+1; } for(j=low;j<i;j++) a[j]=b[j]; } 3 5 3 5 2 6 8 3 5 2 6 8 2 5 r1 3 5 2 6 8 2 3 3 5 2 6 8 2 3 5
  • 9. IN next recursive pass or call to merge 2 3 5 2 3 5 6 8 void merge(int a[], int low, int high, int mid) { a,3,4,3 int b[20],l1,r1,i; l1=low;l1=3 r1=mid+1;r1=4 i=low;i=3 while((l1<=mid) && (r1<=high)) { 3<=3 4<=4 if(a[l1]<a[r1]) { b[i]=a[l1];b[3]=6 l1=l1+1;l1=4 i=i+1;i1=4 } else { b[i]=a[r1]; r1=r1+1; i=i+1; } } while(l1<=mid) { b[i]=a[l1]; l1=l1+1; i=i+1; } while(r1<=high)4<=4 { b[i]=a[r1];b[4]=8 r1=r1+1;r1=5 i=i+1;i=5 } For(j=low;j<i;j++) a[j]=b[j]; } 2 3 5 6 8 2 3 5 2 3 5 6 2 3 5 6 8 2 3 5 6 8
  • 10. IN LAST recursive pass or call to merge 2 3 5 6 8 2 3 5 6 8 void merge(int a[], int low, int high, int mid) { 0,4,2 int b[20],l1,r1,i; l1=low;l1=0 r1=mid+1;r1=3 i=low;i=0 while((l1<=mid) && (r1<=high)) { 0<=2 3<=4 if(a[l1]<a[r1]) { 2<6 b[i]=a[l1];b[0]=2 l1=l1+1;l1=1 i=i+1;i=1 } else { b[i]=a[r1]; r1=r1+1; i=i+1; } } while(l1<=mid) { b[i]=a[l1]; l1=l1+1; i=i+1; } while(r1<=high)3<=4 { b[i]=a[r1];b[3]=6 r1=r1+1;r1=4 i=i+1;i=4 } For(j=low;j<i;j++ a[j]=b[j]; } 2 3 5 6 8 2 3 5 6 8 2 3 5 6 8 2 3 5 6 8 2 3 5 6 8 2 3 5 6 8 2 3 5 6 8 2 3 5 6 8 2 3 5 6 8 2 3 5 6 8
  • 11. FINALLY SORTED ARRAY 2 3 5 6 8