0
I’m a beginner/student in C language and I’m learning about search. I’m having difficulty understanding about the interpolated search, because the test code I passed in the discipline presents error in the calculation of the search.
meio = menor + (maior-menor) * ((arg - vet[menor]) / vet[maior] - vet[menor]);
I found a calculation that presents correct result, but only when the table does not have repeated numbers:
meio = menor + ((maior-menor)/(vet[maior]-vet[menor]))*(arg-vet[menor])
The doubt if interpolated search works in arrangement with repeated numbers? I did not find answers to this question about repeated values in searches, and testing the code in Dev C++ shows error when there are repeated numbers in the interpolated search array.
//Função de busca por interpolação
int buscaInterpol(int vec[5], int tam, int arg){
int menor, maior, meio, achou;
menor = 0;
maior = tam-1;
achou = -1;
while((menor <= maior) && (achou == -1)){
meio = menor + ((maior-menor)/(vec[maior]-vec[menor]))*(arg-vec[menor]);
if (arg == vec[meio]){
achou = meio;
}
if(arg < vec[meio]){
maior = meio - 1;
}
else {
menor = meio + 1;
}
}
return(achou);
}
Thanks for the lesson. But something I didn’t understand yet, ordering is mandatory in every search algorithm, to operate correctly? Or it is an individual characteristic of each search algorithm, and can operate without ordering?
– Santon
It’s something that depends on the characteristics of each algorithm. But imagine what it’s like in the real world, outside of digital: is it easier for you to find something you’re looking for in a mess, or when things already have some sort of tidiness? Similarly, generic search algorithms are less efficient than those assuming that the data is tidied, sorted, or with some other sort of tidying.
– Marcelo Gomes