If I have two functions:
void SortStudents(char *studentList[], size_t studentCount)
{
qsort(studentList, sizeof(studentList)/sizeof(studentList[0]), sizeof(studentList[0]), Compare);
}
int Compare(const void *a, const void *b)
{
return (strcmp(*(char **)a, *(char **)b));
}
That sort and compare using the qsort function, how do I use bsearch to find subsets of my list. For example, if I have two lists:
- (List A) Bob, Jimmy, Lee, James, Anne
- (List B) Jen, Jon, Lee, James, Steph
How do I search in List B to find those elements in A?
Can you also do a search in List B to find those elements not in A?
Thanks.
To do a search, you have to use a one-item list as the key parameter to ‘bsearch()’.
In context, searching for the entry at
a_list[n]inb_list:So, to find the elements in List B that are in List A, you will do:
And to find the elements in B that are not in A, you will need to sort List A after all and then for each element in List B, see whether the element is in List A using the reversed search.
And the output was: