Skip to content

Commit

Permalink
Busca binária 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagorezendev committed Jul 3, 2023
1 parent 0c88c72 commit a0e04ac
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
110 changes: 109 additions & 1 deletion dados.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

FILE *fp;
int HashAcessos;
int D[4817];

void AbrirArquivo() {
Dados Dado;
Expand Down Expand Up @@ -146,11 +147,78 @@ void TelaBBinaria(int chave) {
Borda(12, 14, 25, 2, 0,0);
Borda(82, 14, 25, 2, 0,0);
GotoXY(20, 15); printf("%d Acessos", HashAcessos);
GotoXY(90, 15); printf("%d Acessos", 0 /*BuscaBinaria(chave)*/);
GotoXY(90, 15); printf("%d Acessos", AcessoBinaria(4817, chave));
GotoXY(20, 10);
GotoXY(38, 26); system("PAUSE");
}

/*int BuscaBinaria(int v[], int n, int x){
int ini = 0, fim = n-1;
while(ini <= fim){
int meio = (ini + meio) / 2;
if(x < v[meio]) fim = meio - 1;
else if(x > v[meio]) ini = meio + 1;
else return meio;
}
return -1;
}*/

int AcessoBinaria(int n, int x){
SalvarRank();
int ini = 0, fim = n-1, cont = 1;
while(ini <= fim){
int meio = (ini + meio) / 2;
if(x < D[meio]){
fim = meio - 1;
cont++;
}
else if(x > D[meio]) {
ini = meio + 1;
cont++;
}
else return cont;
}
}

/*int BuscaBinaria(Dados D[], int n, char showId[])
{
int ini = 0, fim = n - 1, meio, acessos = 0;
while (ini <= fim)
{
acessos++;
meio = (ini + fim) / 2;
int comparacao = strcmp(showId, D[meio].Rank);
if (comparacao > 0)
ini = meio + 1;
else if (comparacao < 0)
fim = meio - 1;
else
{
printf("Acessos: %d\n", acessos);
return meio;
}
}
//printf("Acessos: %d\n", acessos);
return acessos;
}*/

/*int AcessoBiQuant(int rank){
Dados D;
int v[4817], i = 0, cont = 0;
FILE *fp = fopen("pldb.csv", "rb");
fseek(fp, 0, SEEK_SET);
while (fread(&D, sizeof(Dados), 1, fp)){
v[i] = D.Rank;
i++;
}
cont = AcessoBinaria(D, 4817, rank);
fclose(fp);
return cont;
}*/

void LerArquivo() {
char linha[100000], texto[100000];
Dados LD;
Expand Down Expand Up @@ -222,3 +290,43 @@ void LerArquivo() {
}
fclose (fporigin);
}

void SalvarRank() {
char linha[100000], texto[100000];
int campo = 0, i = 0, j, tam, t, a, aspas;
FILE *fporigin = fopen("pldb.csv", "r");
if(fporigin == NULL) {
printf("Nao abriu pldb.csv\n");
exit(1);
}
fscanf (fporigin, " %[^\n]", linha);
while (fscanf (fporigin, " %[^\n]", linha)!=EOF) {
memset(&D, 0, sizeof(Dados));
campo = 0;
texto[0] = 0;
tam = strlen(linha);
for(t = 0; t < tam; t++) {
a = 0;
aspas = 0;
while(linha[t] != ',' && t < tam) {
texto[a++] = linha[t++];
if(t > 0 && linha[t-1] == '\"') {
a--;
while(linha[t] != '\"' && t < tam) {
texto[a++] = linha[t++];
}
t++;
}
}
texto[a++] = 0;
switch(campo) {
case 4:
D[i] = atoi (texto);
break;
}
campo++;
}
i++;
}
fclose (fporigin);
}
5 changes: 4 additions & 1 deletion dados.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ void FecharArquivo();
void GravarArquivo(Dados D);
int HashString(char str[]);
Dados Buscar(char Chave[]);
Dados BuscaBinaria(int chave);
//Dados BuscaBinaria(int chave);
void TelaBBinaria();
void Imprimir(Dados D);
void LerArquivo();
int AcessoBiQuant(int id);
int AcessoBinaria(int n, int x);
void SalvarRank();

#endif //CODE_WIKI_DADOS_H

0 comments on commit a0e04ac

Please sign in to comment.