diff --git a/Arquivos/teste.c b/Arquivos/teste.c new file mode 100644 index 0000000000000000000000000000000000000000..85abe842299b0d8ff03cceb5a6fe953597142ead --- /dev/null +++ b/Arquivos/teste.c @@ -0,0 +1,90 @@ +#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"); + +}