Skip to content
Snippets Groups Projects
Commit 6937d6ac authored by clm15@inf.ufpr.br's avatar clm15@inf.ufpr.br
Browse files

so merda!

parent e6db5b09
Branches
No related tags found
No related merge requests found
......@@ -8,3 +8,6 @@ libbusca.a: busca.o
busca.o:
gcc -c busca.c -o busca.o
clean:
rm *.o *.a *.exe
#include "busca.h"
#include <time.h>
int sorteia(int min, int max)
{
int r;
srand(time(NULL));
r = min + (rand() % max);
return r;
}
void vetSort(int *vet, int Tam, int max)
void vetSort(int *vet[], int Tam, int max)
{
int i;
vet = (int *) malloc ((Tam+1) * sizeof(int));
srand(time(NULL));
*vet = (int *) malloc ((Tam+1) * sizeof(int));
for (i = 1; i <= Tam; i++)
vet[i] = sorteia(1, max);
{
(*vet)[i] = 1 + (rand() % max);
printf("Valor %d: %d\n", i, (*vet)[i]);
}
}
void vetcpy(int *vet, int *vet_ord, int Tam)
void vetcpy(int vet[], int *vet_ord[], int Tam)
{
int i;
vet_ord = (int *) malloc ((Tam + 1) * sizeof(int));
*vet_ord = (int *) malloc ((Tam + 1) * sizeof(int));
for (i = 1; i <= Tam; i++)
vet_ord[i] = vet[i];
(*vet_ord[i]) = vet[i];
}
void imprime (int *vet, int N)
{
int i;
for (i = 1; i <= N; i++)
{
printf("Entrou\nTamanho %d\n", N);
for (i = 1; i <= 5; i++)
printf("%d ", i);
/*{
printf("Interacao %d: ", i);
if ((i % 10) == 0)
printf ("\n\n");
printf ("%d ", vet[i]);
}
}*/
}
void jogar (int *vet, int *vet_ord, int N)
void jogar (int *vet[], int *vet_ord[], int N)
{
int x, i = 0, j;
printf ("Entre com a Quadra de valores: ");
for (j = 0; j < 4; j++)
{
scanf ("%d", &x);
if ((pesqseq(vet, x, N) != 0) || (pesqbin(vet_ord, x, N) != 0))
if ((pesqseq(*vet, x, N) != 0) || (pesqbin(*vet_ord, x, N) != 0))
i++;
}
if (i == 4)
......@@ -93,12 +92,13 @@ int main (void)
{
int Tam, max, *vet, *vet_ord;
int menu;
printf ("Entre com tamanho de vetor: ")
printf ("Entre com tamanho de vetor: ");
scanf ("%d", &Tam);
printf ("Entre com valor maximo dos numeros aleatorios: ");
scanf ("%d", &max);
vetSort(vet, Tam, max);
vetcpy(vet, vet_ord, Tam);
vetSort(&vet, Tam, max);
imprime(vet, Tam);
vetcpy(vet, &vet_ord, Tam);
QuickSort_it(vet_ord, Tam);
printf ("\nMenu:\n");
menu = 1;
......@@ -116,7 +116,7 @@ int main (void)
imprime (vet_ord, Tam);
break;
case 4:
jogar (vet, vet_ord, Tam);
jogar (&vet, &vet_ord, Tam);
break;
case 5:
jornada(Tam, max);
......
File added
......@@ -65,11 +65,13 @@ void BubbleSort (int *vet, int N)
int mediana (int *vet, int esq, int dir)
{
int med = (esq+dir)/2;
if (vet[esq] <= vet[dir])
int E = vet[esq], D = vet[dir], M = vet[med];
printf("esq = %d, dir = %d, med = %d\n", E, D, M);
if (E <= D)
{
if (vet[esq] <= vet[med]) //descobrimos se esq é o menor elemento ou não
if (E <= M) //descobrimos se esq é o menor elemento ou não
{
if (vet[med] <= vet[dir]) //se esq é menor elemento, o segundo menor é mediana
if (M <= D) //se esq é menor elemento, o segundo menor é mediana
return med;
else
return dir;
......@@ -79,9 +81,9 @@ int mediana (int *vet, int esq, int dir)
}
else //ja sabemos que esq não é menor elemento, tentamos dir
{
if (vet[dir] <= vet[med])
if (D <= M)
{
if (vet[med] <= vet[esq])
if (M <= E)
return med;
else
return esq;
......@@ -93,7 +95,10 @@ int mediana (int *vet, int esq, int dir)
void Particao (int *vet, int esq, int dir, int *pos_pivo)
{
int i, j, aux, pivo, med = mediana(vet, esq, dir);
int i, j, aux, pivo, med;
printf("Entrou!\n");
med = mediana(vet, esq, dir);
printf("Passou!\n");
pivo = vet[med];
vet[med] = vet[esq];
vet[esq] = pivo;
......@@ -115,6 +120,7 @@ void Particao (int *vet, int esq, int dir, int *pos_pivo)
vet[esq] = vet[j];
vet[j] = pivo;
*pos_pivo = j;
printf("passou!\n");
}
void QuickSort_rec (int *vet, int esq, int dir)
......@@ -123,8 +129,8 @@ void QuickSort_rec (int *vet, int esq, int dir)
if (esq < dir)
{
Particao(vet, esq, dir, &pos_pivo);
QuickSort(vet, esq, pos_pivo - 1);
QuickSort(vet, pos_pivo + 1, dir);
QuickSort_rec(vet, esq, pos_pivo - 1);
QuickSort_rec(vet, pos_pivo + 1, dir);
}
}
......@@ -148,7 +154,7 @@ void QuickSort_it (int *vet, int N)
i++;
pilha_esq[i] = esq;
pilha_dir[i] = pos_pivo - 1;
i++
i++;
pilha_esq[i] = pos_pivo + 1;
pilha_dir[i] = dir;
}
......
busca.o 0 → 100644
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment