guitar-player

mergesort_oficial.c

Feb 1st, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. inline
  7. void merge(int *left, int l_len, int *right, int r_len, int *out)
  8. {
  9.     int i, j, k;
  10.     for (i = j = k = 0; i < l_len && j < r_len; )
  11.         out[k++] = left[i] < right[j] ? left[i++] : right[j++];
  12.  
  13.     while (i < l_len) out[k++] = left[i++];
  14.     while (j < r_len) out[k++] = right[j++];
  15. }
  16.  
  17. /* inner recursion of merge sort */
  18. void recur(int *buf, int *tmp, int len)
  19. {
  20.     int l = len / 2;
  21.     if (len <= 1) return;
  22.  
  23.     /* note that buf and tmp are swapped */
  24.     recur(tmp, buf, l);
  25.     recur(tmp + l, buf + l, len - l);
  26.  
  27.     merge(tmp, l, tmp + l, len - l, buf);
  28. }
  29.  
  30. /* preparation work before recursion */
  31. void merge_sort(int *buf, int len)
  32. {
  33.     /* call alloc, copy and free only once */
  34.     int *tmp = malloc(sizeof(int) * len);
  35.     memcpy(tmp, buf, sizeof(int) * len);
  36.  
  37.     recur(buf, tmp, len);
  38.  
  39.     free(tmp);
  40. }
  41.  
  42. int main(int argc, char *argv[])
  43. {
  44.  
  45.   int n;
  46.   clock_t ini, final;
  47.   double total;
  48.  
  49.  //========
  50.   if(argc == 1)
  51.    {
  52. /*      cout << "Al menos inserte un argumento" << endl;*/
  53.         return 0;
  54.     }
  55.  
  56. char numero[12];
  57. char* p, *q;
  58. for(p = argv[1], q = numero; *p != '\0'; ++p)
  59. {
  60.         if(*p == '.')
  61.         {
  62.                 break;
  63.         }
  64.         else
  65.         {
  66.                 *q = *p;
  67.                 ++q;
  68.         }
  69. }
  70. *q = '\0';
  71.  n = atol(numero);
  72.  /*printf("%d \n", n);*/
  73.  
  74.  
  75.   int *x = malloc(sizeof(int)* n);
  76.  
  77.   FILE *inFile;
  78.   inFile = fopen(argv[1], "r" );
  79.   if (!inFile)
  80.    {
  81.  /*   cout << "Al menos inserte un argumento" << endl;*/
  82.     return 0; //exit(1); // terminate with error
  83.    }
  84.   int k;
  85.   for (k = 0; k < n; k++)
  86.    {
  87.      fscanf(inFile, "%i", &x[k]);    
  88.  
  89.  /*    printf("%i \n", x[k]); */
  90.    }
  91.   fclose(inFile);
  92.  
  93.   //cout << "Inicia Mergesort" << endl;*/
  94.  
  95. /* //=========== */
  96.  
  97. /*
  98.     puts("before sort:");
  99.     for (k = 0; k < n; k++) printf("%d ", x[k]);
  100.     putchar('\n');
  101. */
  102.  ini = clock();
  103.  merge_sort(x, n);
  104.  final=clock();
  105.  
  106.   total=((double)(final - ini)) / CLOCKS_PER_SEC;
  107. /*
  108.   puts("===after sort===:");
  109.     for (k = 0; k < n; k++) printf("%d ", x[k]);
  110.     putchar('\n');
  111. */
  112.    printf ("%d \t",n);
  113.    printf ("%lf \n", total);
  114.  
  115.    
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment