SlideShare a Scribd company logo
MERGE SORT
1
MERGE SORT
ASSIGNMENT OF DATA STRUCTURE
SUBMITTED TO:
Sir Rashid Jahangir
SUBMITTED BY:
Muhammad Abdullah
CIIT/SP14-BSE-B2-005/VHR
COMSATS INSTITUTE OF
INFORMATION & TECHNOLOGY
2
MERGE SORT
HOW THE CODE OF MERGE SORT WORK?
First of all , I shall write the whole program. After then I will try to explain it.
#include <cstdlib>
#include <iostream>
using namespace std;
void merge(int arr[], int low, int high){
int size=(high-low)+1;
int* temp=new int[size];
int mid=(low+high)/2;
int index1=0;
int index2=low;
int index3=mid+1;
while (index2 <= mid && index3 <= high) {
if(arr[index2]<arr[index3]){
temp[index1++]=arr[index2++];
}else{
temp[index1++]=arr[index3++];
}
}
while (index2 <= mid) {
temp[index1] = arr[index2];
index1++;
index2++;
}
while (index3 <= high) {
temp[index1] = arr[index3];
index1++;
index3++;
}
for(int i=0; i<size; i++){
3
MERGE SORT
arr[low+i]=temp[i];
}
delete[] temp;
}
void mergeSort(int arr[],int low, int high){
if(low<high){
int midIndex=(low+high)/2;
mergeSort(arr, low, midIndex);
mergeSort(arr, midIndex+1,high);
merge(arr, low, high); }}
// main is here
int main(int argc, char** argv) {
int size=0;
cout<<"Enter Size of Array"<<endl;
cin>>size;
int* arr= new int[size];
cout<<"Enter Elements of Array"<<endl;
for(int i=0; i<size; i++){
cin>>arr[i];
}
mergeSort(arr, 0, size-1);
cout<<"Sorted Array is"<<endl;
for(int i=0; i < size; i++)
{ cout << arr[i] << " ";} cout<<endl; return 0;}
//The End of code
4
MERGE SORT
We have divided the whole procedure into following steps:
Step 1 :
In step1 we take the size of array from user up to which the array is to
be created. Then user enter the elements of array.
Step 2:
Then we call the merge-sorting function. We pass the array by
reference, initial and last location of array.
Step 3:
He a function is performing different operations. Firstly it check that is
there elements in the array which is passed by to this function. If
Base case condition Recursive calling
5
MERGE SORT
condition is write then the whole array is divided into two parts. Then
left sub-array and right sub-array are continue to divide further it is
called recursively calling itself , until base condition is reached.
The graphical representation is as follows :
In short array is go on to divide itself into two parts one is left-
subarray and second is right-subarray until the size of array becomes 1.
Then at in last statement, sub-arrays are going to merge by calling to
merge function as.
Step 4:
Now we discuss the merge function.
6
MERGE SORT
This function is taking array in which we will copy or merge the two
sub-arrays. Here we also taking the initial and final size of array.
Our logic is that we will copy the two sub-array into temporary created
array. The size of this temp array can be found by subtractioning the
low from high and add 1 as:
Then temp array is created at run-time dynamically as:
Then we assign index2 and mid to initial and final location of left-sub
array. And index3 and high to initial and final of right- sub array as
high
Index2
mid
Index3
7
MERGE SORT
Step 5:
In this step we compare the initial elements of left and right sub-arrays
with each other to find the minimum number. And place them into
temporary array.
Step 6:
Now we can see in the code that
This is written actually for the reason that because the temp array is
not completely gain the elements. Because in step5 condition of while
is not satisfied due to exceeding of index2 or index3 become equal or
become greater than mid or high respectively. So such loop will be
needed for complete transfer of elements to temporary array.
8
MERGE SORT
It is important to note that one of the above while loop will be
executed.
Step 7:
Now we copy the temporary array into original array named as arr.
Then we delete the temporary array to free space.
Step 8:
Now finally at the end we display the sorted array :
THE END

More Related Content

What's hot

Mergesort
MergesortMergesort
Mergesort
sunil kumar
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
University of Science and Technology Chitttagong
 
Continuity of a function module02
Continuity of a function module02Continuity of a function module02
Continuity of a function module02
REYEMMANUELILUMBA
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
Bfs dfs mst
Bfs dfs mstBfs dfs mst
Bfs dfs mst
AvichalVishnoi
 
Quick and radix sort
Quick and radix sortQuick and radix sort
Quick and radix sort
Aaron Joaquin
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
Neil Soliven
 
The JavaScript You Wished You Knew
The JavaScript You Wished You KnewThe JavaScript You Wished You Knew
The JavaScript You Wished You Knew
Troy Miles
 
Advanced Sorting Algorithms
Advanced Sorting AlgorithmsAdvanced Sorting Algorithms
Advanced Sorting Algorithms
Damian T. Gordon
 
Stack - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
Sagacious IT Solution
 
MoneyFormat.sql
MoneyFormat.sqlMoneyFormat.sql
MoneyFormat.sql
Lucas Gutknecht
 
Selection sort
Selection sortSelection sort
Selection sort
Pabelonio Fasoy
 
16 Jop April 08
16 Jop April 0816 Jop April 08
16 Jop April 08
Ganesh Samarthyam
 
Chapter 14 quick_sort
Chapter 14 quick_sortChapter 14 quick_sort
Chapter 14 quick_sort
Terry Yoast
 
Workshop 04 Review
Workshop 04 ReviewWorkshop 04 Review
Workshop 04 Review
migiwara
 
Selection sort
Selection sortSelection sort
Selection sort
stella D
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav2910
 
Pop operation
Pop operationPop operation
Pop operation
Rajesh K Shukla
 
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
University of Science and Technology Chitttagong
 
Circular linked list
Circular linked list Circular linked list
Circular linked list
sajinis3
 

What's hot (20)

Mergesort
MergesortMergesort
Mergesort
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Continuity of a function module02
Continuity of a function module02Continuity of a function module02
Continuity of a function module02
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Bfs dfs mst
Bfs dfs mstBfs dfs mst
Bfs dfs mst
 
Quick and radix sort
Quick and radix sortQuick and radix sort
Quick and radix sort
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
 
The JavaScript You Wished You Knew
The JavaScript You Wished You KnewThe JavaScript You Wished You Knew
The JavaScript You Wished You Knew
 
Advanced Sorting Algorithms
Advanced Sorting AlgorithmsAdvanced Sorting Algorithms
Advanced Sorting Algorithms
 
Stack - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
 
MoneyFormat.sql
MoneyFormat.sqlMoneyFormat.sql
MoneyFormat.sql
 
Selection sort
Selection sortSelection sort
Selection sort
 
16 Jop April 08
16 Jop April 0816 Jop April 08
16 Jop April 08
 
Chapter 14 quick_sort
Chapter 14 quick_sortChapter 14 quick_sort
Chapter 14 quick_sort
 
Workshop 04 Review
Workshop 04 ReviewWorkshop 04 Review
Workshop 04 Review
 
Selection sort
Selection sortSelection sort
Selection sort
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Pop operation
Pop operationPop operation
Pop operation
 
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
 
Circular linked list
Circular linked list Circular linked list
Circular linked list
 

Viewers also liked

Data Structure Sorting
Data Structure SortingData Structure Sorting
Data Structure Sorting
Muhazzab Chouhadry
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topic
Saddam Hussain
 
Sorting
SortingSorting
Sorting
Ghaffar Khan
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
Merge sort
Merge sortMerge sort
Merge sort
Vidushi Pathak
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
multimedia9
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
geeortiz
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 

Viewers also liked (8)

Data Structure Sorting
Data Structure SortingData Structure Sorting
Data Structure Sorting
 
Bubble sort a best presentation topic
Bubble sort a best presentation topicBubble sort a best presentation topic
Bubble sort a best presentation topic
 
Sorting
SortingSorting
Sorting
 
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
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 

Similar to Working of Merge Sort Code

Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
Dr. Mohammad Amir Khusru Akhtar (Ph.D)
 
Data Structure Marge sort Group 5 pptx so
Data Structure Marge sort Group 5 pptx soData Structure Marge sort Group 5 pptx so
Data Structure Marge sort Group 5 pptx so
Salma368452
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
maamir farooq
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
Micheal Ogundero
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxUnit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docx
dickonsondorris
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
ArjunSingh81957
 
Lewis jssap3 e_labman02
Lewis jssap3 e_labman02Lewis jssap3 e_labman02
Lewis jssap3 e_labman02
auswhit
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
Ashim Sikder
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
skilljiolms
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
ambikavenkatesh2
 
Ds
DsDs
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
siennatimbok52331
 
Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docx
dunhamadell
 
RTOS.pptx
RTOS.pptxRTOS.pptx
RTOS.pptx
IrshanNazir3
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
Eman magdy
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stack
ecomputernotes
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
RenalthaPujaBagaskar
 

Similar to Working of Merge Sort Code (20)

Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
Data Structure Marge sort Group 5 pptx so
Data Structure Marge sort Group 5 pptx soData Structure Marge sort Group 5 pptx so
Data Structure Marge sort Group 5 pptx so
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxUnit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docx
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
 
Lewis jssap3 e_labman02
Lewis jssap3 e_labman02Lewis jssap3 e_labman02
Lewis jssap3 e_labman02
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
Ds
DsDs
Ds
 
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
 
Objectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docxObjectives Assignment 09 Applications of Stacks COS.docx
Objectives Assignment 09 Applications of Stacks COS.docx
 
RTOS.pptx
RTOS.pptxRTOS.pptx
RTOS.pptx
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stack
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 

Recently uploaded

Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
Prakhyath Rai
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
ijaia
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
harshapolam10
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICSUNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
vmspraneeth
 
Operational amplifiers and oscillators notes
Operational amplifiers and oscillators notesOperational amplifiers and oscillators notes
Operational amplifiers and oscillators notes
ShachiPGowda
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 

Recently uploaded (20)

Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...Software Engineering and Project Management - Software Testing + Agile Method...
Software Engineering and Project Management - Software Testing + Agile Method...
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICSUNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
 
Operational amplifiers and oscillators notes
Operational amplifiers and oscillators notesOperational amplifiers and oscillators notes
Operational amplifiers and oscillators notes
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 

Working of Merge Sort Code

  • 2. 1 MERGE SORT ASSIGNMENT OF DATA STRUCTURE SUBMITTED TO: Sir Rashid Jahangir SUBMITTED BY: Muhammad Abdullah CIIT/SP14-BSE-B2-005/VHR COMSATS INSTITUTE OF INFORMATION & TECHNOLOGY
  • 3. 2 MERGE SORT HOW THE CODE OF MERGE SORT WORK? First of all , I shall write the whole program. After then I will try to explain it. #include <cstdlib> #include <iostream> using namespace std; void merge(int arr[], int low, int high){ int size=(high-low)+1; int* temp=new int[size]; int mid=(low+high)/2; int index1=0; int index2=low; int index3=mid+1; while (index2 <= mid && index3 <= high) { if(arr[index2]<arr[index3]){ temp[index1++]=arr[index2++]; }else{ temp[index1++]=arr[index3++]; } } while (index2 <= mid) { temp[index1] = arr[index2]; index1++; index2++; } while (index3 <= high) { temp[index1] = arr[index3]; index1++; index3++; } for(int i=0; i<size; i++){
  • 4. 3 MERGE SORT arr[low+i]=temp[i]; } delete[] temp; } void mergeSort(int arr[],int low, int high){ if(low<high){ int midIndex=(low+high)/2; mergeSort(arr, low, midIndex); mergeSort(arr, midIndex+1,high); merge(arr, low, high); }} // main is here int main(int argc, char** argv) { int size=0; cout<<"Enter Size of Array"<<endl; cin>>size; int* arr= new int[size]; cout<<"Enter Elements of Array"<<endl; for(int i=0; i<size; i++){ cin>>arr[i]; } mergeSort(arr, 0, size-1); cout<<"Sorted Array is"<<endl; for(int i=0; i < size; i++) { cout << arr[i] << " ";} cout<<endl; return 0;} //The End of code
  • 5. 4 MERGE SORT We have divided the whole procedure into following steps: Step 1 : In step1 we take the size of array from user up to which the array is to be created. Then user enter the elements of array. Step 2: Then we call the merge-sorting function. We pass the array by reference, initial and last location of array. Step 3: He a function is performing different operations. Firstly it check that is there elements in the array which is passed by to this function. If Base case condition Recursive calling
  • 6. 5 MERGE SORT condition is write then the whole array is divided into two parts. Then left sub-array and right sub-array are continue to divide further it is called recursively calling itself , until base condition is reached. The graphical representation is as follows : In short array is go on to divide itself into two parts one is left- subarray and second is right-subarray until the size of array becomes 1. Then at in last statement, sub-arrays are going to merge by calling to merge function as. Step 4: Now we discuss the merge function.
  • 7. 6 MERGE SORT This function is taking array in which we will copy or merge the two sub-arrays. Here we also taking the initial and final size of array. Our logic is that we will copy the two sub-array into temporary created array. The size of this temp array can be found by subtractioning the low from high and add 1 as: Then temp array is created at run-time dynamically as: Then we assign index2 and mid to initial and final location of left-sub array. And index3 and high to initial and final of right- sub array as high Index2 mid Index3
  • 8. 7 MERGE SORT Step 5: In this step we compare the initial elements of left and right sub-arrays with each other to find the minimum number. And place them into temporary array. Step 6: Now we can see in the code that This is written actually for the reason that because the temp array is not completely gain the elements. Because in step5 condition of while is not satisfied due to exceeding of index2 or index3 become equal or become greater than mid or high respectively. So such loop will be needed for complete transfer of elements to temporary array.
  • 9. 8 MERGE SORT It is important to note that one of the above while loop will be executed. Step 7: Now we copy the temporary array into original array named as arr. Then we delete the temporary array to free space. Step 8: Now finally at the end we display the sorted array : THE END