Skip to content
Snippets Groups Projects
Commit c445c829 authored by Fernando Erd's avatar Fernando Erd :ok_hand:
Browse files

QUICK SORT FUNCIONANDO PORRA

parent 7e192dec
No related branches found
No related tags found
No related merge requests found
...@@ -14,30 +14,56 @@ void printVetor( int Vetor[], int n ) { ...@@ -14,30 +14,56 @@ void printVetor( int Vetor[], int n ) {
printf( "%d ", Vetor[i] ); printf( "%d ", Vetor[i] );
printf ("\n"); printf ("\n");
} }
int Mediana (int Esquerda, int Meio, int Direita) {
if ((Esquerda >= Direita && Esquerda <= Meio) || (Esquerda >= Meio && Esquerda <= Direita)) int Mediana(int *Vetor, int esq, int dir) {
return Esquerda; int aux;
if (( Direita >= Esquerda && Direita <= Meio) || (Direita >= Meio && Direita <= Esquerda)) aux = (dir+esq)/2; // elem. do meio do vetor
return Direita;
else //mediana dos 3 numeros
return Meio;
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 Esquerda, int Direita) { int Particao (int *Vetor, int esq, int dir) {
int Pivo, i, j; int i,j,pivo,aux;
aux = Mediana(Vetor,esq,dir);
pivo = Vetor[aux];
printf("%d\n",pivo);
i = esq;
j = dir;
while (i < j) {
Pivo = Vetor[Direita]; while ( (Vetor[i] <= pivo) && (i < dir) )
i = (Esquerda - 1);
for (j = Esquerda; j <= Direita- 1; j++)
{
if (Vetor[j] <= Pivo)
{
i++; i++;
Troca (Vetor, i, j);
} while (Vetor[j] > pivo)
j--;
if (i < j)
Troca(Vetor,j,i);
} }
Troca (Vetor,i + 1, Direita);
return (i+1); printVetor(Vetor,18);
Vetor[esq] = Vetor[j];
Vetor[j] = pivo;
return j;
} }
void QuickSortRecursivo (int Vetor[], int Esquerda, int Direita) { void QuickSortRecursivo (int Vetor[], int Esquerda, int Direita) {
...@@ -79,8 +105,9 @@ void QuickSortIterativo (int Vetor[], int Esquerda, int Direita) { ...@@ -79,8 +105,9 @@ void QuickSortIterativo (int Vetor[], int Esquerda, int Direita) {
} }
int main() { int main() {
int Vetor[] = {51, 2, 45, 95, 43, 78, 12, 32}; int Vetor[] = {1,2,3,4,5,8,6,4,43,43,56,23,43,4,3,75,342,532};
int n = sizeof( Vetor ) / sizeof( *Vetor ); int n = sizeof( Vetor ) / sizeof( *Vetor );
QuickSortRecursivo( Vetor, 0, n - 1 ); printVetor (Vetor, n);
QuickSortIterativo( Vetor, 0, n - 1 );
printVetor( Vetor, n ); printVetor( Vetor, n );
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment