Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. // QUICKSORT FUNCAO //
  2. int quickSORT(vetor * vec, int left, int right){
  3. int i, j, tamanho=right-left+1, posicao;
  4. char * aux;
  5. // com tamanho 0 ou 1 esta ordenado
  6. if(left>=right ) return 0;
  7. else {
  8. int pos = rand()%tamanho + left; // determina pivot
  9. // colocar pivot no fim
  10. aux= vec->elementos[pos].str;
  11. vec->elementos[pos].str = vec->elementos[right].str;
  12. vec->elementos[right].str = aux;
  13. // passo de parti��o
  14. i = left; j = right-1;
  15. while(1) {
  16. while (strcmp(vec->elementos[i].str,vec->elementos[right].str)< 0 && i < right) i++;
  17. while (strcmp(vec->elementos[right].str, vec->elementos[j].str)< 0 && j >= 0) j--;
  18. if (i < j){
  19. //trocar vec->elementos[i].str com vec->elementos[j].str
  20. aux= vec->elementos[i].str;
  21. vec->elementos[i].str = vec->elementos[j].str;
  22. vec->elementos[j].str = aux;
  23. }
  24.  
  25. }
  26. //reposi�ao do pivot
  27. //caso os elementos centrais sejam maiores que o pivot
  28. if(strcmp(vec->elementos[i].str, vec->elementos[right].str)>0 && strcmp(vec->elementos[j].str, vec->elementos[right].str)>0){
  29. aux= vec->elementos[i].str;
  30. vec->elementos[i].str = vec->elementos[right].str;
  31. vec->elementos[right].str = aux;
  32. posicao=i;}
  33. //caso os elementos centrais sejam menores que o pivot
  34. if(strcmp(vec->elementos[i].str, vec->elementos[right].str)<0 && strcmp(vec->elementos[j].str, vec->elementos[right].str)<0){
  35. aux= vec->elementos[j+1].str;
  36. vec->elementos[j+1].str = vec->elementos[right].str;
  37. vec->elementos[right].str = aux;
  38. posicao=j+1;}
  39. //caso um seja maior e outro menor ou entao iguais
  40. else{
  41. //caso os elementos estejam desordenados
  42. if(strcmp(vec->elementos[i].str, vec->elementos[j].str)>0){
  43. aux= vec->elementos[i].str;
  44. vec->elementos[i].str = vec->elementos[j].str;
  45. vec->elementos[j].str = aux;}
  46. //trocar o pivot de posi�ao com o valor mais baixo
  47. aux= vec->elementos[i].str;
  48. vec->elementos[i].str = vec->elementos[right].str;
  49. vec->elementos[right].str = aux;
  50. posicao=i;}
  51. //ordenar o sub-vector a esquerda do elemento ordenado
  52. quickSORT(vec, left, posicao-1);
  53. //ordenar o sub-vector a direita do elemento ordenado
  54. quickSORT(vec, posicao + 1 , right);
  55. }
  56. }
  57.  
  58.  
  59. int vetor_ordena_qsort(vetor* vec){
  60. int left = 0, right = vetor_tamanho(vec);
  61. quickSORT(vec, left, right);
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement