Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<time.h>
- #include<stdlib.h>
- void bubblesort(int a[100000],int n)
- {
- int i,j,temp;
- for(i=0;i<n-1;i++)
- {
- for(j=0;j<n-1;j++)
- {
- if(a[j]>a[j+1])
- {
- temp = a[j];
- a[j] = a[j+1];
- a[j+1] = temp;
- }
- }
- }
- return;
- }
- void insertsort(int c[100000],int n)
- {
- int i,j,temp;
- for(i=1;i<n;i++)
- {
- temp=c[i];
- j=i-1;
- while((temp<c[j])&&(j>=0))
- {
- c[j+1]=c[j];
- j=j-1;
- }
- c[j+1]=temp;
- }
- }
- void selectsort(int b[100000],int n)
- {
- int i,j,min,pos;
- for(i=0;i<n-1;i++)
- {
- min = b[i];
- pos = i;
- for(j=i+1;j<n;j++)
- {
- if(b[j]<min)
- {
- min = b[j];
- pos = j;
- }
- }
- b[pos] = b[i];
- b[i] = min;
- }
- }
- int main()
- {
- int nextrand,n,a[100000],b[100000],c[100000],i;
- clock_t bubbletime1,bubbletime2,inserttime1,inserttime2,selecttime1,selecttime2;
- double bubbletime,inserttime,selecttime;
- FILE *fp;
- fp = fopen("C:/data.txt","w");
- printf("Enter the size of array ");
- scanf("%d",&n);
- for(i=0;i<n;i++)
- {
- nextrand = rand();
- nextrand = nextrand%1000;
- fprintf(fp,"%d",nextrand);
- }
- fclose(fp);
- fp = fopen("C:/data.txt","r");
- for(i=0;i<n;i++)
- fscanf(fp,"%d",&a[i]);
- for(i=0;i<n;i++)
- {
- c[i] = a[i];
- b[i] = a[i];
- }
- bubbletime1 = clock();
- bubblesort(a,n);
- bubbletime2 = clock();
- selecttime1 = clock();
- selectsort(b,n);
- selecttime2 = clock();
- inserttime1 = clock();
- insertsort(c,n);
- inserttime2 = clock();
- bubbletime = (double) (bubbletime2 - bubbletime1)/CLOCKS_PER_SEC;
- selecttime = (double) (selecttime2 - selecttime1)/CLOCKS_PER_SEC;
- inserttime = (float) (inserttime2 - inserttime1)/CLOCKS_PER_SEC;
- printf("\ntime taken by bubble sort is %f",bubbletime);
- printf("\ntime taken by selection sort is %f",selecttime);
- printf("\ntime taken by insertion sort is %f",inserttime);
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement