Advertisement
Guest User

Selection-Sort

a guest
Jul 7th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include <sys/time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #define MAX 65536
  6.  
  7. int main(int argc, char**argv)
  8. {
  9.     double start;
  10.     struct timeval tm;
  11.     double duration; // gibt die Laufzeit
  12.     int k,i,x,tmp,a[MAX];
  13.     unsigned int seed; 
  14.     FILE* urandom = fopen("/dev/urandom", "r");
  15.     for(i=0;i<MAX;i++)
  16.     {
  17.         fread(&seed, sizeof(int), 1, urandom);
  18.         a[i] = seed;
  19.     }  
  20.     fclose(urandom);
  21.     gettimeofday(&tm,NULL);
  22.     start = (double) (tm.tv_sec) + ((double) (tm.tv_usec))/1.0e6;
  23.     for(k=0;k<MAX-1;k++)
  24.     {
  25.         i=k;
  26.         for(x=(k+1);x<MAX;x++)
  27.         {
  28.             if(a[x] < a[i])
  29.                 i = x;
  30.         }
  31.         tmp = a[i];
  32.         a[i] = a[k];
  33.         a[k] = tmp;
  34.     }
  35.  
  36.     gettimeofday(&tm,NULL);
  37.     duration = (double) (tm.tv_sec) + ((double) (tm.tv_usec))/1.0e6 - start;
  38.     for(i=0;i<MAX;i++)
  39.     {
  40.         printf("%d\n",a[i]);
  41.     }
  42.     printf("\nLaufzeit: %lf\n",duration);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement