Write a C program that implements a simple array-based insertion sort. Your program must read in the integers from the accompanying data file and use insertion sort to store the sorted data into an array. If you must insert an element between two existing values, then you must also move (or shift) the elements all elements with an index >= the index where you wish to insert the new element. Note that you can find the insertion sort algorithm in the textbook and the slides.
This is the ints from the text file. The ints are under each other and not the way they are shown here: 879 646 80 385 741 57 370 240 111 400 262 678 951 506 720 508 792 863 677 864 70 5 591 440 989 478 867 636 278 827 692 243 806 676 158 550 425 226 783 129 876 714 125 721 164 555 730 146 596 947 174 837 48 589 808 868 694 677 379 62 580 165 956 139 215 14 45 552 98 154 702 661 997 825 363 782 229 915 281 397 295 219 231 476 253 22 873 504 653 698 772 184 453 508 977 863 624 947 104 926
This the code I have for now. I am getting the addresses in order but now the integers from the file. When i comment out the insertionSort function, the numbers print fine but obviously not in order. What am i doing wrong?
#include <stdio.h>
#include<stdio.h>
void insertionSort(int *, int);
int main()
{
int i,num,array[1000];
FILE *fp = fopen("data_a5.txt","r");
fscanf(fp,"%d",&num);
if(fp== NULL)
{
printf("Error reading File!\n");
return;
}
while(!feof(fp))
{
fscanf(fp,"%d", &array[num]);
//printf("%d\n", array[num]);
}
insertionSort(array,num);
for(i=0;i<num;i++)
printf("%d\n",&array[i]);
fclose(fp);
return 0;
}
void insertionSort(int *value, int size)
{
int i,j,temp;
for(i = 0; i <= size; i++)
{
for(j = i; j >= 0; j--)
{
if(value[j+1]<value[j])
{
temp=value[j+1];
value[j+1]=value[j];
value[j]=temp;
}
else
break;
}
}
}