I'm writing a code that is supposed to search for a key, in my case a last name, and then using a binary search attempt to find this key in a different file containing information( First name, Last name, birthday etc..). This information is in a structure called StudentRecord, and is sorted alphabetically by last name using a quicksort function. I cant get the binary code function to work properly. I either get " segmentation fault 11" or name not found when it should be found.
void binary_search( struct StudentRecord *records[], int start, int
finish, char key[20]){
int i;
int mid;
start=0;
finish=i-1;
while(start<=finish)
{
mid=(start+finish)/2;
if (strcmp(key,records[mid]->LastNames)==0)
{
printf("key found at the position %d\n",mid+1);
exit(0);
}
else if(strcmp(key,records[mid]->LastNames)>0)
{
finish=finish;
start=mid+1;
binary_search(records, start,finish,key);
}
else if (strcmp(key,records[mid]->LastNames)<0)
{
start=start;
finish=mid-1;
binary_search(records, start, finish, key);
}
}
printf("name not found\n");
}
Aucun commentaire:
Enregistrer un commentaire