Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. struct bucket
  2. {
  3.     int count;
  4.     int* value;
  5. };
  6.  
  7. int compareIntegers(const void* first, const void* second)
  8. {
  9.     int x = *((int*)first), y = *((int*)second);
  10.     if (x == y)
  11.     {
  12.         return 0;
  13.     }
  14.     else if (x < y)
  15.     {
  16.         return -1;
  17.     }
  18.     else
  19.     {
  20.         return 1;
  21.     }
  22. }
  23. void Bucket_sort(int array[], int n, int contador, Resul* x)
  24. {
  25.  
  26.     time_t t_ini, t_fim;
  27.     long long int swaps=0, compara=0;
  28.  
  29.  
  30.  
  31.     struct bucket buckets[3];
  32.     int i, j, k;
  33.     for (i = 0; i< 3; i++){
  34.         buckets[i].count = 0;
  35.         buckets[i].value = (int*) malloc(sizeof(int) * n);
  36.     }
  37.     for (i = 0; i<n; i++){
  38.         compara++;
  39.         if (array[i] < 0)
  40.         {
  41.             buckets[0].value[buckets[0].count++] = array[i];
  42.         }
  43.         compara++;
  44.         else if (array[i] > 10)
  45.         {
  46.             buckets[2].value[buckets[2].count++] = array[i];
  47.         }
  48.         else
  49.         {
  50.             buckets[1].value[buckets[1].count++] = array[i];
  51.         }
  52.     }
  53.     for (k = 0, i = 0; i< 3; i++){
  54.         // now using quicksort to sort the elements of buckets
  55.         qsort(buckets[i].value, buckets[i].count, sizeof(int), &compareIntegers);
  56.         for (j = 0; j<buckets[i].count; j++){
  57.             array[k + j] = buckets[i].value[j];
  58.         }
  59.         k += buckets[i].count;
  60.         free(buckets[i].value);
  61.     }
  62.  
  63.  
  64.   t_fim = time(NULL);
  65.  
  66. x[contador].tempo = difftime(t_ini, t_fim);
  67. x[contador].swaps = swaps;
  68.   x[contador].compara = compara;
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement