Created
April 12, 2023 00:57
-
-
Save emanoelbarreiros/376136c7d1d5a1298249a0a6473ad42b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Quicksort { | |
public static void ordenar(int[] vetor, int inicio, int fim) { | |
if (inicio < fim) { | |
int particao = particionar(vetor, inicio, fim); | |
ordenar(vetor, inicio, particao - 1); | |
ordenar(vetor, particao + 1, fim); | |
} | |
} | |
public static void inverter(int[] vetor, int pos1, int pos2) { | |
int temp = vetor[pos1]; | |
vetor[pos1] = vetor[pos2]; | |
vetor[pos2] = temp; | |
} | |
public static int particionar(int[] vetor, int inicio, int fim) { | |
int x = vetor[fim]; | |
int i = inicio - 1; | |
for (int j = inicio; j < fim; j++) { | |
if (vetor[j] < x) { | |
i++; | |
inverter(vetor, i, j); | |
} | |
} | |
inverter(vetor, i + 1, fim); | |
return i + 1; | |
} | |
public static void main(String[] args) { | |
int[] v = {1,5,2,6,8,3,4}; | |
ordenar(v, 0, v.length - 1); | |
System.out.println(Arrays.toString(v)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment