diff --git a/busca.c b/busca.c index 7c7dd9b74b281e213c5f15d48756ef72002372f9..b4679ffd517a87ef870dc1132d44ce5b181fdba7 100644 --- a/busca.c +++ b/busca.c @@ -114,7 +114,7 @@ void Particao (int *vet, int esq, int dir, int *pos_pivo) *pos_pivo = j; } -void QuickSort (int *vet, int esq, int dir) +void QuickSort_rec (int *vet, int esq, int dir) { int pos_pivo; if (esq < dir) @@ -124,3 +124,30 @@ void QuickSort (int *vet, int esq, int dir) QuickSort(vet, pos_pivo + 1, dir); } } + +void QuickSort_it (int *vet, int N) +{ + int esq, dir, pos_pivo; + int i, *pilha_esq, *pilha_dir; + pilha_esq = (int *) malloc (N*sizeof(int)); + pilha_dir = (int *) malloc (N*sizeof(int)); + pilha_esq[0] = 1; + pilha_dir[0] = N; + i = 0; + while (i >= 0) + { + esq = pilha_esq[i]; + dir = pilha_dir[i]; + i--; + if (esq < dir) + { + Particao(vet, esq, dir, &pos_pivo); + i++; + pilha_esq[i] = esq; + pilha_dir[i] = pos_pivo - 1; + i++ + pilha_esq[i] = pos_pivo + 1; + pilha_dir[i] = dir; + } + } +} diff --git a/busca.h b/busca.h index 7231fd441893540068c7e172fcdf5e86c21975cc..e85b2a46b35cabeeeb34557aca62aa3ac2f3388c 100644 --- a/busca.h +++ b/busca.h @@ -2,6 +2,7 @@ #define BUSCA_H #include <stdio.h> +#include <stdlib.h> int pesqseq (int *, int , int ); int pesqbin (int *, int , int ); @@ -10,6 +11,7 @@ void SelectSort (int *, int ); void BubbleSort (int *, int ); int mediana (int *, int , int ); void Particao (int *, int , int , int *); -void QuickSort (int *, int, int); +void QuickSort_rec (int *, int, int); +void QuickSort_it (int *, int); #endif