Please read the question carefully and program this in C In
Please read the question carefully and program this in C:
In this assignment, we will implement the merge sort algorithm that works on the array of integers. Your program should accept user’s input for the array size, generate random list of integers, and output original list and sorted one. You should submit main.c file.
Solution
#include <stdio.h>
#include <stdlib.h>
void merging(int myarr[],int les,int midle,int higher);
void divide(int myarr[],int les,int higher);
int main(void) {
int limit;
int i,r;
printf(\"enter limit:\");
scanf(\"%d\",&limit);
int list[limit];
printf(\"original list is:\");
for(i=0;i<limit;i++){
r=rand()%10;
list[i]=r;
printf(\"\\t%d\",list[i]);
}
printf(\"\ \");
divide(list,0,limit-1);
printf(\"sorted one:\");
for(i=0;i<limit;i++)
printf(\"\\t%d \",list[i]);
return 0;
}
void divide(int myarr[],int les,int higher){
int midle;
if(les<higher){
midle=(les+higher)/2;
divide(myarr,les,midle);
divide(myarr,midle+1,higher);
merging(myarr,les,midle,higher);
}
}
void merging(int myarr[],int les,int midle,int higher){
int j,x,y,z,tempval[20];
z=les;
j=les;
x=midle+1;
while((z<=midle)&&(x<=higher)){
if(myarr[z]<=myarr[x]){
tempval[j]=myarr[z];
z++;
}
else{
tempval[j]=myarr[x];
x++;
}
j++;
}
if(z>midle){
for(y=x;y<=higher;y++){
tempval[j]=myarr[y];
j++;
}
}
else{
for(y=z;y<=midle;y++){
tempval[j]=myarr[y];
j++;
}
}
for(y=les;y<=higher;y++){
myarr[y]=tempval[y];
}
}
output:
enter limit:5
original list is: 3 6 7 5 3
sorted one: 3 3 5 6 7

