Question: In C programming, The insertion sort algorithm updates its sorted array as each new data value is entered. It is an in-place algorithm, in that
In C programming,
The insertion sort algorithm updates its sorted array as each new data value is entered. It is an in-place algorithm, in that the sorted array at each step writes over the array from the previous step.
Let us start with sorting in descending order (largest to smallest). The ascending order case follows as a simple modification to the descending case.
Assume that we have thus far read N values and these are sorted in correct order (largest to smallest) in our array. We then read the next number, and we desire to place it in our array in the proper position. In order to do so, we compare the new input with the existing array values, starting with the last array element. If the new input has a value greater than the last array element, shift the array element to the next higher index (i.e., copy the N-th element to the N+1-th element). Continue to compare with lower indices; as long as the new input has a greater value than the current element, copy that element to the next higher index. At some point, the input value may not be greater than the array element; in this case, break out of the element-by-element testing and replace the previously compared element by the new input.
Program Flow Prior to your main processing loop, you should:
Explain the program to the user by calling a function that prints the explanation to the screen.
Ask the user as to whether the sort is to be in ascending or descending order. Store this choice in a flag variable that will later be passed into your sorting function.
Open the file data.txt that contains the data to be sorted. Create the file yourself.
Check to make sure the file exists.
Main Processing Loop:
Read the next value from your input file. Assume that the maximum number of input points is 50. Use a Macro to define this maximum size. If EOF is encountered your program should terminate reading the file. If a value is read, continue to the next step.
Assuming that there was a new input to read, call your insertion sort function and pass as arguments the number of points thus far sorted, the new input point, the ascending/descending flag and a reference (pointer) to the sorted array.
Using the insertion sort algorithm, place the new point in its proper location in the array, and return back to your main function. Do not read or display anything from your function.
Increment the number of points thus far sorted.
Print out your thus-far sorted array in a horizontal fashion to the screen (see example).
Repeat step 1.
Example
Let the input file contain the following numbers: {-5.2, 3.7, -11.9, 4.4, 3.7, 20.2, -6.0, 8.2, 14.6, -20.6}. Assuming that you are sorting from largest to smallest, your output should look like the below text:
-5.2 3.7 -5.2 3.7 -5.2 -11.9 4.4 3.7 -5.2 -11.9 4.4 3.7 3.7 -5.2 -11.9 20.2 4.4 3.7 3.7 -5.2 -11.9 20.2 4.4 3.7 3.7 -5.2 -6 -11.9 20.2 8.2 4.4 3.7 3.7 -5.2 -6 -11.9 20.2 14.6 8.2 4.4 3.7 3.7 -5.2 -6 -11.9 20.2 14.6 8.2 4.4 3.7 3.7 -5.2 -6 -11.9 -20.6
This is the code to edit
#include
#include
#define SIZE 100
void explain(void);
int main(void)
{
FILE *fp;
char filename[20];
int i, SortChoice,DataLength=0;
double data[SIZE]={0}, CurrentNumber;
explain();
scanf("%s", filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("File data.txt does not exist ");
exit(1);
}
printf("Would you like to sort in Ascending (1) or Descending (2) order? ");
scanf("%d",&SortChoice);
/*Add Code Here*/
return 0;
}
void explain(void)
{
/*Insert Program explanation here*/
}
/* Add Other Functions Here */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
