Analyze this code and input descriptive comments explaining what is happening in
ID: 3869114 • Letter: A
Question
Analyze this code and input descriptive comments explaining what is happening in clear english
please provide comments only on code after the line
void merge(int a[],int i1,int j1,int i2,int j2)
the entire code is as follows
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf(" Sorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;
while(i<=j1 && j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
//Transfer elements from temp[] back to a[]
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
Explanation / Answer
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i; // declaring variables
printf("Enter no of elements:");
scanf("%d",&n); // reading no. of elements
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]); // reading array elements from user
// calling mergesort() with the array, the lower limit and the upper limit
mergesort(a,0,n-1);
printf(" Sorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]); // printing array elements
return 0;
}
void mergesort(int a[],int i,int j)
{
/* Merge sort is a divide and conquer technique to sort an array.
* It first divides the array in two halves and sort each half
* separately and then merges the two halves into one.
* Now, after dividing the array into two halves, it follows the
* aforesaid method on each of the half i.e., it repeats the divide
* and sort then merge process on each of the halves created until
* the halves cannot the divided anymore. After that, it starts merging
* them. Merge sort uses recursion technique to achieve it's goal.
*/
int mid; // mid will hold the middle point of the array
if(i<j) // recursion will be done only if lower limit is smaller than upper limit
// i: lower limit, j: upper limit
{
mid=(i+j)/2; // calculating mid point of array
mergesort(a,i,mid); // calling mergesort() on first half of the array to sort
mergesort(a,mid+1,j); // calling mergesort() on last half of the array to sort
merge(a,i,mid,mid+1,j); // after sorting separate halves, merge() will merge the two halves
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;
//j1 holds end of first list, j2 holds end of second list
while(i<=j1 && j<=j2) // when lower limits of both lists will not cross the upper limits
{
if(a[i]<a[j]) // if element from first list is smaller than element from second list
temp[k++]=a[i++]; // then store element from first list first
else
temp[k++]=a[j++]; // else store element from second list first
}
// when one of the two lists exhausted while merging, then the remaining elements
// from the other list should be inserted after them
while(i<=j1) // if first list has not been exhausted
temp[k++]=a[i++]; // insert them into the array
while(j<=j2) // if second list has not been exhausted
temp[k++]=a[j++]; // insert them into the array
//Transfer elements from temp[] back to a[]
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}