Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. int swap(char *x, char * y){
  2.  
  3.     char string[1000];
  4.  
  5.         strcpy(string, x);
  6.       strcpy(x, y);
  7.       strcpy(y, string);
  8.  
  9.     return 0;
  10. }
  11.  
  12. int vetor_ordena_qsort(vetor* vec){
  13.  
  14.     int *left=0;
  15.     int *right=vec->tamanho-1;
  16.  
  17.     int i, j, tamanho = right-left+1;
  18.  
  19.     if (tamanho<2) {
  20.         return;
  21.     }
  22.  
  23.     else {
  24.         int pos = rand()%tamanho + left; //determina pivot aleatorio.
  25.         swap(&vec->elementos[pos].str, &vec->elementos[right].str); //coloca o pivot no fim.
  26.         i = left;   j = right-1; //passo de partiçao.
  27.  
  28.         while (1) {
  29.             while (strcmp(vetor_elemento(vec, i), vetor_elemento(vec, right))<0 && i < right) i++;
  30.             while (strcmp(vetor_elemento(vec, right), vetor_elemento(vec, j))<0 && j>=0) j--;
  31.  
  32.             if (i < j) swap(&vec->elementos[i].str, &vec->elementos[j].str);
  33.             else break;
  34.         }
  35.  
  36.         swap(&vec->elementos[i].str, &vec->elementos[right].str);
  37.         vetor_ordena_qsort(vec);
  38.         vetor_ordena_qsort(vec, i+1, right);
  39.     }
  40.  
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement