Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Program- Mergesort (the negatives (-) in the examples means left side is open.

ID: 3884356 • Letter: C

Question


C Program- Mergesort
(the negatives (-) in the examples means left side is open..)

1. Design, code, and test a C program to mergesort a set of intervals of positive rational numbers. The first line of the input will be , the number of intervals in the remaining n input lines. Each input interval will have four non-zero integer values: num_left den_left num_right den_right. If num_left is positive, then the left end of the interval em ie oherwise the left end of the interal i open ate iet . Similarly, if num_right is is closed at otherwise the left end of the interval is open at den le den_left interval is closed at numright otherwise the right end of the interval i positive, then the right end of the interval is closed at otherwise the right end of the interval is open at den_right' num right den_left and den_right will always be positive, each fraction is in reduced form, and every den_ right interval includes at least one number The input should be read from standard input (which will be one of 1. keyboard typing, 2. a shell redirect (

Explanation / Answer

the code so far what i done

#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); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging 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;
  
while(i<=j1 && j<=j2) //while elements in both lists
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
  
while(i<=j1) //copy remaining elements of the first list
temp[k++]=a[i++];
  
while(j<=j2) //copy remaining elements of the second list
temp[k++]=a[j++];
  
//Transfer elements from temp[] back to a[]
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}