Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Quicksort {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- int quantEntrada = Integer.parseInt(s.nextLine());
- String entrada = s.nextLine();
- String[] vetor = entrada.split("\\|");
- int[] vetor1 = new int[quantEntrada];
- for (int i = 0; i < quantEntrada; i++) {
- vetor1[i] = Integer.parseInt(vetor[i]);
- }
- System.out.println(Arrays.toString(ordenacao(vetor1, 0, quantEntrada - 1)));
- }
- public static int[] ordenacao(int[] vetor, int valorI, int valorF) {
- if (valorI < valorF) {
- int posicaoPivo = particao(vetor, valorI, valorF);
- ordenacao(vetor, valorI, posicaoPivo - 1);
- ordenacao(vetor, posicaoPivo + 1, valorF);
- }
- return vetor;
- }
- public static int particao(int[] vetor, int valorI, int valorF) {
- int pivo = vetor[valorI];
- int i = valorI + 1;
- int j = valorF;
- while (i <= j) {
- if (vetor[i] <= pivo)
- i++;
- else if (pivo < vetor[j])
- j--;
- else {
- int troca = vetor[i];
- vetor[i] = vetor[j];
- vetor[j] = troca;
- i++;
- j--;
- }
- }
- vetor[valorI] = vetor[j];
- vetor[j] = pivo;
- return j;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement