Add comments to decribe what exactly is happening in this program. Be sure the c
ID: 3812370 • Letter: A
Question
Add comments to decribe what exactly is happening in this program.
Be sure the comments are readable and understandable for someone with minimal programming expirience.
The more comments, the better.
#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
Program
#include<stdio.h> // Standard input output header
void mergesort(int a[],int i,int j); // mergesort Function declaration
void merge(int a[],int i1,int j1,int i2,int j2); // merge Function declaration
int main() // main Function with return type int (integer value)
{
int a[30],n,i; // local variables declaration
printf("Enter no of elements:"); // Printf statement displaying following text
scanf("%d",&n); // No: of elements value is store in "n" let it be 5
printf("Enter array elements:"); // Printf statement displaying following text
for(i=0;i<n;i++) // for loop started with i=0,i<5(initial value of n),i++
scanf("%d",&a[i]); // Read the value in an array
mergesort(a,0,n-1); // Function call with variables a,0,n-1
printf(" Sorted array is :"); // Printf statement displaying following text
for(i=0;i<n;i++)
printf("%d ",a[i]); // It will print the array elements based on condition
return 0; // it will return 0
}
void mergesort(int a[],int i,int j) // function have a,0,n-1 initially
{
int mid; // local variable declaration
if(i<j) // if condition that validates i<j(i points to first i.e a[0] and j points to last i.e a[4])
{
mid=(i+j)/2; // calculating value of mid
mergesort(a,i,mid); // Recursion chain to sort first half of the array
mergesort(a,mid+1,j); // Recursion chain to sort Second half of the array
merge(a,i,mid,mid+1,j); // merge of two sorted sub arrays
}
}
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; //initializing k value
while(i<=j1 && j<=j2) // check both the conditions if they are true enter the loop
{
if(a[i]<a[j]) // check the condition with array elements at that i,j positions
temp[k++]=a[i++]; // temp[k++] stores the value of a[i++]
else // if the above condition fails
temp[k++]=a[j++]; // temp[k++] stores the value of a[j++]
}
while(i<=j1) // check the condition if it is true enter the loop
temp[k++]=a[i++];
while(j<=j2) // check the condition if it is true enter the loop
temp[k++]=a[j++];
//Transfer elements from temp[] back to a[]
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
As you stated I added comments to the code.start the process by taking some sample values and proceed as per the program. If you still need any ?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.