Uses the binary search algorithm to make an efficient search for an item in a sorted array.
#include <stdlib.h> void *bsearch( const void *key, const void *base, size_t num, size_t size, int (*compare) (const void *, const void *));
key
Search criteria.
base
The array to be searched.
num
The number of elements in the array.
size
The size of each element in the array.
compare
A pointer to a comparison function.
The key argument points to the item you want to search for.
The base argument points to the first element of the array to search. This array must already be sorted in ascending order, based on the comparison requirements of the function pointed to by the compare argument.
The compare argument is a pointer to a programmer-supplied function that bsearch() calls to compare two elements of the array. This comparison function takes two element pointers as arguments. If the element that the first argument points is equal to the element that the second argument points to, the comparison function must return 0. If the first argument is less than the second, the comparison function must return a negative number. If the first argument is greater than the second argument, the function must return a positive number.
bsearch() returns a pointer to the element in the array matching the item pointed to by key. If no match was found, bsearch() returns a null pointer ( NULL ).
/* A simple telephone directory manager. This program accepts a list of names and telephone numbers, sorts the list, then searches for specified names. */ #include <stdlib.h> #include <stdio.h> #include <string.h> /* Maximum number of records in the directory. */ #define MAXDIR 40 /* Telephone directory record */ typedef struct direntry{ char lname[15]; /* Key field, see comp(). */ char fname[15]; char phone[15]; } direntry; int comp(const direntry *, const direntry *); direntry *look(char *); direntry directory[MAXDIR]; int reccount = 0; int main(void) { direntry *next = directory; direntry *ptr = NULL; char lookstr[15]; printf("Telephone directory program.\n"); printf("Enter blank last name when done.\n"); do { printf("\nLast name: "); gets(next->lname); if (strlen(next->lname) == 0) break; printf("First name: "); gets(next->fname); printf("Phone number: "); gets(next->phone); ++reccount; } while (reccount < MAXDIR)); printf("Thank you. Now sorting. . .\n"); /* Sort the array using qsort(). */ qsort(directory, reccount, sizeof(direntry), comp); printf("Enter last name to search for,\n"); printf("blank to quit.\n"); printf("\nLast name: "); gets(lookstr); while (strlen(lookstr) > 0) { ptr = look(lookstr); if (ptr != NULL) printf( "%s, %s: %s\n", ptr->lname, ptr->fname, ptr->phone); else printf("Can't find %s.\n", lookstr); printf("\nLast name: "); gets(lookstr); } printf("Done.\n"); return 0; } int comp(const direntry *rec1, const direntry *rec2) { return (strcmp((char *)rec1->lname, (char *)rec2->lname)); } /* Search through the array using bsearch() */ direntry *look(char k[]) { return (direntry *) bsearch(k, directory, reccount, sizeof(direntry), comp); } Output Telephone directory program. Enter blank last name when done. Last name: Mation First name: Infor Phone number: 555-1212 Last name: Bell First name: Alexander Phone number: 555-1111 Last name: Johnson First name: Betty Phone number: 555-1010 Last name: Thank you. Now sorting. . . Enter last name to search for, blank to quit. Last name: Mation Infor, Mation: 555-1212 Last name: Johnson Johnson, Betty: 555-1010 Last name: Done.