diff --git a/Arquivos/QuickSort.c b/Arquivos/QuickSort.c new file mode 100644 index 0000000000000000000000000000000000000000..ce74cdaa889805da0810b088482a0f1357c06f86 --- /dev/null +++ b/Arquivos/QuickSort.c @@ -0,0 +1,73 @@ +#include <stdio.h> + +void Troca ( int* a, int* b ) { + int t = *a; + *a = *b; + *b = t; +} + +int Particao (int Vetor[], int Esquerda, int Direita) { + int Pivo = Vetor[Direita]; + int i = (Esquerda - 1); + + for (int j = Esquerda; j <= Direita- 1; j++) + { + if (Vetor[j] <= Pivo) + { + i++; + Troca (&Vetor[i], &Vetor[j]); + } + } + Troca (&Vetor[i + 1], &Vetor[Direita]); + return (i + 1); +} + +void QuickSortRecursivo (int Vetor[], int Esquerda, int Direita) { + int pospivo; + if (Esquerda < Direita) { + pospivo = Particao(Vetor, Esquerda, Direita); + QuickSortRecursivo(Vetor,Esquerda,pospivo-1); + QuickSortRecursivo(Vetor,pospivo+1,Direita); + } +} +void QuickSortIterativo (int Vetor[], int Esquerda, int Direita) { + int Posicao; + int Pilha[ Direita - Esquerda + 1 ]; + int top; + + top = -1; + Pilha[ ++top ] = Esquerda; + Pilha[ ++top ] = Direita; + + while ( top >= 0 ) + { + Direita = Pilha[ top-- ]; + Esquerda = Pilha[ top-- ]; + + Posicao = Particao( Vetor, Esquerda, Direita ); + + if ( Posicao-1 > Esquerda ) + { + Pilha[ ++top ] = Esquerda; + Pilha[ ++top ] = Posicao - 1; + } + + if ( Posicao+1 < Direita ) + { + Pilha[ ++top ] = Posicao + 1; + Pilha[ ++top ] = 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 n = sizeof( Vetor ) / sizeof( *Vetor ); + QuickSortRecursivo( Vetor, 0, n - 1 ); + printVetor( Vetor, n ); +} diff --git a/Arquivos/Quicksort_interativo.c b/Arquivos/Quicksort_interativo.c deleted file mode 100644 index 442849dc99acd9e97ec9cb2f76a17b035e366bce..0000000000000000000000000000000000000000 --- a/Arquivos/Quicksort_interativo.c +++ /dev/null @@ -1,66 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> - -typedef struct EstruturaPilha{ - int Posicao; - struct EstruturaPilha *Prox; -} EstruturaPilha; - -typedef struct { - EstruturaPilha *Topo; -} TPilha; - -void InicPilha (TPilha *Pilha) { - Pilha->Topo = (EstruturaPilha*) malloc (sizeof(EstruturaPilha)); - Pilha->Topo->Prox = NULL; -} - -int VaziaPilha (TPilha *Pilha) { - return (Pilha->Topo->Prox == NULL); -} - -void Push (TPilha *Pilha,int elemento) { - EstruturaPilha *Aux; - - Aux = (EstruturaPilha*) malloc (sizeof (EstruturaPilha)); - Aux->Prox = Pilha->Topo; - Pilha->Topo->Posicao = elemento; - Pilha->Topo = Aux; -} - -void Pop (TPilha *Pilha, int *elemento) { - EstruturaPilha *Aux; - Aux = Pilha->Topo; - Pilha->Topo = Aux->Prox; - free (Aux); - *elemento = Pilha->Topo->Posicao; -} - -void QuickSort_Interativo (int *Vetor, int TAM) { - TPilha PilhaDireita, PilhaEsquerda; - int Esquerda, Direita, i, j; - - InicPilha (&PilhaDireita); - InicPilha (&PilhaEsquerda); - Esquerda = 0; - Direita = TAM - 1; - - Push (&PilhaDireita, Direita); - Push (&PilhaEsquerda, Esquerda); - - do { - if (Direita > Esquerda) { - //Particiona (Vetor, Esquerda, Direita, &i, &j); - Push (&PilhaDireita, j); - Push (&PilhaEsquerda, Esquerda); - Esquerda = i; - } else { - Pop (&PilhaDireita, &Direita); - Pop (&PilhaEsquerda, &Esquerda); - } - } while (!VaziaPilha (&PilhaDireita)); -} - -main() { - int n, TAM; -} diff --git a/Arquivos/teste.c b/Arquivos/teste.c deleted file mode 100644 index 85abe842299b0d8ff03cceb5a6fe953597142ead..0000000000000000000000000000000000000000 --- a/Arquivos/teste.c +++ /dev/null @@ -1,90 +0,0 @@ -#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"); - -}