Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. int _fat_partition_(vetor* vec, int left, int right)
  2.  
  3. {
  4.  
  5.     int tamanho = right-left+1;
  6.  
  7.  
  8.  
  9.     if (vec == NULL)
  10.  
  11.     {
  12.  
  13.         return -1;
  14.  
  15.     }
  16.  
  17.  
  18.  
  19.     else if (tamanho < 2)
  20.  
  21.     {
  22.  
  23.         return 0;
  24.  
  25.     }
  26.  
  27.     if(vec->tamanho < 150) // InsertionSort
  28.  
  29.     {
  30.  
  31.         int i, j;
  32.  
  33.         char* aux;
  34.  
  35.         for(i = 1; i < vec->tamanho; i++)
  36.  
  37.         {
  38.  
  39.             aux = vec->elementos[i].str;
  40.  
  41.             for(j = i; j>0 && strcmp(aux, vec->elementos[j-1].str) < 0; j--)
  42.  
  43.             {
  44.  
  45.                 strcpy(vec->elementos[j].str, vec->elementos[j-1].str);
  46.  
  47.             }
  48.  
  49.             strcpy(vec->elementos[j].str, aux);
  50.  
  51.         }
  52.  
  53.     }
  54.  
  55.     else
  56.  
  57.     {
  58.  
  59.         /* ESCOLHA DE PIVOT - ORDENAÇÃO DOS VALORES LEFT, MIDDLE E RIGHT PARA
  60.  
  61.         MELHORAR A EFICACIA DO ALGORITMO*/
  62.  
  63.         int pos = (left+right)/2;
  64.  
  65.         if(strcmp(vec->elementos[right].str, vec->elementos[left].str) < 0)
  66.  
  67.         {
  68.  
  69.             _swap_(vec, left, right);
  70.  
  71.         }
  72.  
  73.         if(strcmp(vec->elementos[pos].str, vec->elementos[left].str) < 0)
  74.  
  75.         {
  76.  
  77.             _swap_(vec, left, pos);
  78.  
  79.         }
  80.  
  81.         if(strcmp(vec->elementos[right].str, vec->elementos[pos].str) < 0)
  82.  
  83.         {
  84.  
  85.             _swap_(vec, pos, right);
  86.  
  87.         }
  88.  
  89.         /* DUTCH NATIONAL FLAG PROBLEM - 3 way partition*/
  90.  
  91.         int left_values = left, right_values = right - 1;
  92.  
  93.         int i;
  94.  
  95.         for(i = left +1; i <= right_values; )
  96.  
  97.         {
  98.  
  99.             if(strcmp(vec->elementos[i].str, vec->elementos[left].str) < 0)
  100.  
  101.             {
  102.  
  103.                 _swap_(vec, left_values, i);
  104.  
  105.                 left_values++;
  106.  
  107.             }
  108.  
  109.             if(strcmp(vec->elementos[i].str, vec->elementos[left].str) > 0)
  110.  
  111.             {
  112.  
  113.                 _swap_(vec, right_values, i);
  114.  
  115.                 right_values--;
  116.  
  117.             }
  118.  
  119.             else
  120.  
  121.             {
  122.  
  123.                 i++;
  124.  
  125.             }
  126.  
  127.  
  128.  
  129.         }
  130.  
  131.         if(left_values > left)
  132.  
  133.         {
  134.  
  135.             _fat_partition_(vec, left, left_values-1);
  136.  
  137.         }
  138.  
  139.           if(right_values < right)
  140.  
  141.         {
  142.  
  143.           _fat_partition_(vec, right_values+1, right);
  144.  
  145.         }
  146.  
  147.     }
  148.  
  149.     return 0;
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement