Binary Searching 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,pos,value,first,last,mid;
	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("Searching Process started........");
	start=clock();
	printf("Enter a Numner \n");
	scanf("%d",&value);
	for(i=1;i<=n;i++){
		first=1;
		last=n;
		while(first<=last){
			mid=(first+last)/2;
			if(value==data[i])
			{
				pos=1;
				break;
			}else{
				if(value>data[i])
					first=mid+1;
				else
					last=mid-1;

			}
		}

	}
	end = ((double)clock()-start)/CLOCK_PER_SEC;
	printf("Searching process compleated.\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 'output.txt'\n");
	fprintf(fpl,"%d %fpl",n,mid);



}

Hits: 10

S M Mohaimanul Islam

Mad man loving technology.

Leave a Reply

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