Advertisement
Viniciusfelbs

Quicksort

Oct 8th, 2020
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Quicksort {
  5.     public static void main(String[] args) {
  6.         Scanner s = new Scanner(System.in);
  7.         int quantEntrada = Integer.parseInt(s.nextLine());
  8.         String entrada = s.nextLine();
  9.         String[] vetor = entrada.split("\\|");
  10.         int[] vetor1 = new int[quantEntrada];
  11.         for (int i = 0; i < quantEntrada; i++) {
  12.             vetor1[i] = Integer.parseInt(vetor[i]);
  13.         }
  14.         System.out.println(Arrays.toString(ordenacao(vetor1, 0, quantEntrada - 1)));
  15.     }
  16.  
  17.     public static int[] ordenacao(int[] vetor, int valorI, int valorF) {
  18.         if (valorI < valorF) {
  19.             int posicaoPivo = particao(vetor, valorI, valorF);
  20.             ordenacao(vetor, valorI, posicaoPivo - 1);
  21.             ordenacao(vetor, posicaoPivo + 1, valorF);
  22.         }
  23.         return vetor;
  24.     }
  25.  
  26.     public static int particao(int[] vetor, int valorI, int valorF) {
  27.         int pivo = vetor[valorI];
  28.         int i = valorI + 1;
  29.         int j = valorF;
  30.         while (i <= j) {
  31.             if (vetor[i] <= pivo)
  32.                 i++;
  33.             else if (pivo < vetor[j])
  34.                 j--;
  35.             else {
  36.                 int troca = vetor[i];
  37.                 vetor[i] = vetor[j];
  38.                 vetor[j] = troca;
  39.                 i++;
  40.                 j--;
  41.             }
  42.         }
  43.         vetor[valorI] = vetor[j];
  44.         vetor[j] = pivo;
  45.         return j;
  46.     }
  47. }
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement