a[r] = temp[r];
r--;
}
}
void insertionsort(float a[], int l, int h) {
int i;
for (i = l; i <= h; i++) {
int j = i - 1, val = a[i];
while (j >= 0 && a[j]>val) {
a[j + 1] = a[j];
j--;
}
a[++j] = val;
}
}
void mergesort(float a[], float temp[], int l, int r) {
if (r - l>8) {
int mid = l + (r - l) / 2;
mergesort(a, temp, l, mid);
mergesort(a, temp, mid + 1, r);
merge(a, temp, l, mid + 1, r);
}
else {
insertionsort(a, l, r);
}
}
int main() {
//CHEGGEA, declare file poiters for reading and writing to file
FILE *fp1, *fp2;
float arr[10], temp[10];
printf("Enter element of array ");
int i;
//CHEGGEA, open input file for reading, output file for writing
fp1 = fopen("data1.txt", "r");
//check if file is open
if (!fp1)
{
printf("Not able to open input file data1.txt ");
return -1;
}
//file open successful , now you can read file and fill up the array.,CHEGGEA, as a generailsed form
for (i = 0; i<10; i++) {
temp[i] = 0;
fscanf(fp1, "%f", &arr[i]);
//scanf("%f", &arr[i]);
}
//open file for writinhg to file out1.,CHEGGEA
fp2 = fopen("out1.txt", "w");
if (!fp2)
{
printf("Not able to open file out1.txt for writinng ");
return -1;
}
//replace all print with frpintf to write to a file called out1.txt,CHEGGEA
fprintf(fp2,"%s"," your array : ");
for (i = 0; i<10; i++)
fprintf(fp2,"%.2f ", arr[i]);
mergesort(arr, temp, 0, 9);
fprintf(fp2,"%s"," your sorted array : ");
for (i = 0; i < 10; i++)
//commented out below line to write to file called out1.txt
//printf("%.2f ", arr[i]);
fprintf(fp2, "%.2f ", arr[i]);
fclose(fp1);
fclose(fp2);
return 0;
}