Skip to content
Snippets Groups Projects
Commit a1b8041b authored by Gabriel Ruschel's avatar Gabriel Ruschel
Browse files

Arquivo de teste do Quicksort recursivo (ainda não funcionando)

parent cfbfe3e0
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
void Troca (int *Vetor, int i, int j) {
int aux;
aux = Vetor[i];
Vetor [i] = Vetor[j];
Vetor [j] = aux;
}
int Mediana(int *Vetor, int esq, int dir) {
int aux;
aux = (dir+esq)/2; // elem. do meio do vetor
//mediana dos 3 numeros
if ( (Vetor[esq] >= Vetor[aux]) && (Vetor[esq] <= Vetor[dir]) )
return esq;
else if ( (Vetor[esq] >= Vetor[dir]) && (Vetor[esq] <= Vetor[aux]) )
return esq;
else if ( (Vetor[dir] >= Vetor[aux]) && (Vetor[dir] <= Vetor[esq]) )
return dir;
else if ( (Vetor[dir] >= Vetor[esq]) && (Vetor[dir] <= Vetor[aux]) )
return dir;
else if ( (Vetor[aux] >= Vetor[esq]) && (Vetor[aux] <= Vetor[dir]) )
return aux;
else if ( (Vetor[aux] >= Vetor[dir]) && (Vetor[aux] <= Vetor[esq]) )
return aux;
}
int Particao (int *Vetor, int esq, int dir) {
int i,j,pivo,aux;
aux = Mediana(Vetor,esq,dir);
pivo = Vetor[aux];
printf("%d\n",pivo);
i = esq;
j = dir;
while (i < j) {
while ( (Vetor[i] <= pivo) && (i < dir) )
i++;
while (Vetor[j] > pivo)
j--;
if (i < j)
Troca(Vetor,j,i);
}
Vetor[aux] = Vetor[j];
Vetor[j] = pivo;
return j;
}
void QuickSortRecursivo(int *Vetor, int esq, int dir) {
int pospivo;
if (esq < dir) {
pospivo = Particao(Vetor, esq, dir);
QuickSortRecursivo(Vetor,esq,pospivo-1);
QuickSortRecursivo(Vetor,pospivo+1,dir);
}
}
int main() {
int i;
int vetor[9];
printf("Vetor desordenado: \n");
for (i=0; i<9; i++) {
vetor[i] = rand() % 20;
printf("%d ",vetor[i]);
}
printf("\n");
QuickSortRecursivo(vetor,0,8);
printf("Vetor ordenado:\n");
for (i=0; i<9; i++)
printf("%d ",vetor[i]);
printf("\n");
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment