Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var a = [6, 1, 8, 2, 7];
- function quickSort(vetor, inicio, fim) {
- if (inicio < fim) {
- div = particiona(vetor, inicio, fim);
- quickSort(vetor, inicio, div);
- quickSort(vetor, div+1, fim);
- }
- return vetor;
- }
- function particiona(vetor, inicio, fim) {
- var posPivo = Math.floor(vetor.length / 2);
- while (inicio < fim)
- {
- do {
- fim--;
- } while (vetor[fim] <= vetor[posPivo]);
- do {
- inicio++;
- } while (vetor[inicio] >= vetor[posPivo]);
- if (vetor[inicio] < vetor[fim]) {
- troca(vetor, inicio, fim);
- }
- }
- return fim;
- }
- function troca (vetor, inicio, fim) {
- let aux = vetor[inicio];
- vetor[inicio] = vetor[fim];
- vetor[fim] = vetor[inicio];
- }
- a = quickSort(a, 0, a.length-1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement