Insertion Sort With Time Complexity In C

#include<stdio.h>
#include<time.h>
#define CLOCK_PER_SEC 1000
long int data[50000];
int main(){
	
	long int n=50000,i,j,value;
	double end;
	clock_t start;
	FILE *fp;
	FILE *fpl;
	fp=fopen("Documentinput.txt","r");
	fpl=fopen("output.txt","a");
	fscanf(fp,"%d",&n);
	printf("Reading data from file\n");
	for(i=1;i<=n;i++){
		fscanf(fp,"%d",data[i]);
	}
	printf("Data Reading completed.\n");
	printf("Sorting process started..........\n");
	start=clock();

	for(j=2;j<=n;j++){
		value=data[j];
		i=j-1;
		while(i>0 && data[i]>value){
			data[i+1]=i;
			i=i-1;
			data[i+1]=value;
		}
	}
	end=((double)clock()-start)/CLOCK_PER_SEC;
	printf("Sorting process completed\n");
	printf("No. of Data......|......Time needed for searching.\n");
	printf("%d\t\t\t%lf\n",n,end);
	printf("This information is added to file 'output.txt'");
	fprintf(fpl,"%d %lf",n,end);
	
}

Hits: 19

S M Mohaimanul Islam

Mad man loving technology.

Leave a Reply

Your email address will not be published. Required fields are marked *