Advertisement
renix1

tentativa de um quick sort fracassado

Mar 22nd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var a = [6, 1, 8, 2, 7];
  2.  
  3. function quickSort(vetor, inicio, fim) {
  4.     if (inicio < fim) {
  5.         div = particiona(vetor, inicio, fim);
  6.         quickSort(vetor, inicio, div);
  7.         quickSort(vetor, div+1, fim);
  8.     }
  9.     return vetor;
  10. }
  11.  
  12. function particiona(vetor, inicio, fim) {
  13.     var posPivo = Math.floor(vetor.length / 2);
  14.     while (inicio < fim)
  15.     {
  16.         do {
  17.             fim--;
  18.         } while (vetor[fim] <= vetor[posPivo]);
  19.  
  20.         do {
  21.             inicio++;
  22.         } while (vetor[inicio] >= vetor[posPivo]);
  23.  
  24.         if (vetor[inicio] < vetor[fim]) {
  25.             troca(vetor, inicio, fim);
  26.         }
  27.     }
  28.     return fim;
  29. }
  30.  
  31. function troca (vetor, inicio, fim) {
  32.     let aux = vetor[inicio];
  33.     vetor[inicio] = vetor[fim];
  34.     vetor[fim] = vetor[inicio];
  35. }
  36.  
  37. a = quickSort(a, 0, a.length-1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement