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

Bug Mediana

parent f60f8146
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
void Troca ( int* a, int* b ) {
int t = *a;
*a = *b;
*b = t;
void Troca (int *Vetor, int i, int j) {
int aux;
aux = Vetor[i];
Vetor [i] = Vetor[j];
Vetor [j] = aux;
}
void printVetor( int Vetor[], int n ) {
int i;
for ( i = 0; i < n; ++i )
printf( "%d ", Vetor[i] );
printf ("\n");
}
int Mediana (int Esquerda, int Meio, int Direita) {
if ((Esquerda >= Direita && Esquerda <= Meio) || (Esquerda >= Meio && Esquerda <= Direita))
return Esquerda;
if (( Direita >= Esquerda && Direita <= Meio) || (Direita >= Meio && Direita <= Esquerda))
return Direita;
else
return Meio;
}
int Particao (int Vetor[], int Esquerda, int Direita) {
int Pivo = Vetor[Direita];
int Pivo = Mediana (Vetor[Esquerda], Vetor[Esquerda+Direita/2+1], Vetor[Direita]);
int i = (Esquerda - 1);
for (int j = Esquerda; j <= Direita- 1; j++)
......@@ -15,10 +32,11 @@ int Particao (int Vetor[], int Esquerda, int Direita) {
if (Vetor[j] <= Pivo)
{
i++;
Troca (&Vetor[i], &Vetor[j]);
Troca (Vetor, i, j);
}
}
Troca (&Vetor[i + 1], &Vetor[Direita]);
Troca (Vetor,i + 1, Direita);
printVetor (Vetor, 8);
return (i+1);
}
......@@ -59,14 +77,9 @@ void QuickSortIterativo (int Vetor[], int Esquerda, int Direita) {
}
}
}
void printVetor( int Vetor[], int n ) {
int i;
for ( i = 0; i < n; ++i )
printf( "%d ", Vetor[i] );
}
int main() {
int Vetor[] = {4, 3, 5, 2, 1, 3, 2, 3};
int Vetor[] = {51, 2, 45, 95, 43, 78, 12, 32};
int n = sizeof( Vetor ) / sizeof( *Vetor );
QuickSortRecursivo( Vetor, 0, n - 1 );
printVetor( Vetor, n );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment