Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. public static void quicksort(int vect[]) {
  2.  
  3.     int[] vetor = vect;
  4.     quickSort(vetor, 0, vetor.length - 1);
  5.  
  6. }
  7.  
  8. private static void quickSort(int[] vetor, int inicio, int fim) {
  9.     if (inicio < fim) {
  10.         int posicaoPivo = separar(vetor, inicio, fim);
  11.         quickSort(vetor, inicio, posicaoPivo - 1);
  12.         quickSort(vetor, posicaoPivo + 1, fim);
  13.     }
  14. }
  15.  
  16. private static int separar(int[] vetor, int inicio, int fim) {
  17.     int pivo = vetor[inicio];
  18.     int i = inicio + 1, f = fim;
  19.     while (i <= f) {
  20.         if (vetor[i] <= pivo)
  21.             i++;
  22.         else if (pivo < vetor[f])
  23.             f--;
  24.         else {
  25.             int troca = vetor[i];
  26.             vetor[i] = vetor[f];
  27.             vetor[f] = troca;
  28.             i++;
  29.             f--;
  30.         }
  31.     }
  32. vetor[inicio] = vetor[f];
  33. vetor[f] = pivo;
  34. return f;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement