Advertisement
MuryloMiranda

QuickSort

Mar 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. const int tam = 10;
  8. typedef int tvet[tam];
  9.  
  10. void QuickSort(tvet &vet, int inicio, int fim)
  11. {
  12. int i = inicio, j = fim;
  13. int aux;
  14. int pivot = vet[(inicio + fim) / 2];
  15.  
  16. while (i <= j)
  17. {
  18. while (vet[i] < pivot)
  19. i++;
  20. while (vet[j] > pivot)
  21. j--;
  22. if (i <= j)
  23. {
  24. aux = vet[i];
  25. vet[i] = vet[j];
  26. vet[j] = aux;
  27. i++;
  28. j--;
  29. }
  30. }
  31.  
  32. if (inicio < j)
  33. QuickSort(vet, inicio, j);
  34. if (i < fim)
  35. QuickSort(vet, i, fim);
  36. }
  37.  
  38. void ExibeVetor(tvet vet)
  39. {
  40. for(int i = 0; i < tam; i++)
  41. cout << vet[i] << " ";
  42. }
  43.  
  44. int main()
  45. {
  46. tvet Vetor;
  47. int i;
  48. srand(time(NULL));
  49.  
  50. for(i = 0; i < tam; i++)
  51. {
  52. Vetor[i+1] = Vetor[i];
  53. Vetor[i] = rand()%50;
  54. }
  55.  
  56. cout << "Os valores do vetor sao:" << endl;
  57. for(i = 0; i < tam; i++)
  58. cout << Vetor[i] << " ";
  59.  
  60. QuickSort(Vetor, 0, tam - 1);
  61.  
  62. cout << "\nOs valores do vetor ordenado sao:" << endl;
  63.  
  64. ExibeVetor(Vetor);
  65. system("PAUSE");
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement