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

Working of Merge Sort Code

  • 1.
  • 2.
    1 MERGE SORT ASSIGNMENT OFDATA 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 THECODE 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; } voidmergeSort(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 havedivided 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 iswrite 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 functionis 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: Inthis 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 isimportant 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