Advertisement
d1i2p3a4k5

aoa p1

Jan 15th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<stdlib.h>
  4. void bubblesort(int a[100000],int n)
  5. {
  6.     int i,j,temp;
  7.     for(i=0;i<n-1;i++)
  8.     {
  9.         for(j=0;j<n-1;j++)
  10.         {
  11.             if(a[j]>a[j+1])
  12.              {
  13.                 temp = a[j];
  14.                 a[j] = a[j+1];
  15.                 a[j+1] = temp;
  16.             }
  17.         }
  18.     }
  19.     return;
  20. }
  21. void insertsort(int c[100000],int n)
  22. {
  23.     int i,j,temp;
  24.      for(i=1;i<n;i++)
  25.     {
  26.       temp=c[i];
  27.       j=i-1;
  28.       while((temp<c[j])&&(j>=0))
  29.         {
  30.             c[j+1]=c[j];
  31.             j=j-1;
  32.         }
  33.       c[j+1]=temp;
  34.     }
  35. }
  36. void selectsort(int b[100000],int n)
  37. {
  38.     int i,j,min,pos;
  39.     for(i=0;i<n-1;i++)
  40.     {
  41.         min = b[i];
  42.         pos = i;
  43.         for(j=i+1;j<n;j++)
  44.         {
  45.             if(b[j]<min)
  46.             {
  47.                 min = b[j];
  48.                 pos = j;
  49.             }
  50.         }
  51.         b[pos] = b[i];
  52.         b[i] = min;
  53.     }
  54. }
  55. int  main()
  56. {
  57.     int nextrand,n,a[100000],b[100000],c[100000],i;
  58.     clock_t bubbletime1,bubbletime2,inserttime1,inserttime2,selecttime1,selecttime2;
  59.     double bubbletime,inserttime,selecttime;
  60.     FILE *fp;
  61.     fp = fopen("C:/data.txt","w");
  62.     printf("Enter the size of array  ");
  63.     scanf("%d",&n);
  64.     for(i=0;i<n;i++)
  65.     {
  66.         nextrand = rand();
  67.         nextrand = nextrand%1000;
  68.         fprintf(fp,"%d",nextrand);
  69.     }
  70.     fclose(fp);
  71.     fp = fopen("C:/data.txt","r");
  72.     for(i=0;i<n;i++)
  73.         fscanf(fp,"%d",&a[i]);
  74.     for(i=0;i<n;i++)
  75.     {
  76.         c[i] = a[i];
  77.         b[i] = a[i];
  78.     }
  79.     bubbletime1 = clock();
  80.     bubblesort(a,n);
  81.     bubbletime2 = clock();
  82.     selecttime1 = clock();
  83.     selectsort(b,n);
  84.     selecttime2 = clock();
  85.     inserttime1 = clock();
  86.     insertsort(c,n);
  87.     inserttime2 = clock();
  88.     bubbletime = (double) (bubbletime2 - bubbletime1)/CLOCKS_PER_SEC;
  89.     selecttime = (double) (selecttime2 - selecttime1)/CLOCKS_PER_SEC;
  90.     inserttime = (float) (inserttime2 - inserttime1)/CLOCKS_PER_SEC;
  91.     printf("\ntime taken by bubble sort is %f",bubbletime);
  92.     printf("\ntime taken by selection sort is %f",selecttime);
  93.     printf("\ntime taken by insertion sort is %f",inserttime);
  94.     fclose(fp);
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement