Just as qsort() was the easiest sorting method, because it is part of the standard library, bsearch() is the
easiest searching method to use
easiest searching method to use
Following is the prototype for bsearch():
void *bsearch(const void *key, const void *buf, size_t num, size_t size,
int (*comp)(const void *, const void *));
The bsearch() function performs a binary search on an array of sorted data elements. A binary search is another “divide and conquer” algorithm. The key is compared with the middle element of the array. If it isequal, the search is done. If it is less than the middle element, the item searched for must be in the first half of the array, so a binary search is performed on just the first half of the array. If the key is greater than the middle element, the item searched for must be in the second half of the array, so a binary search is performed on just the second half of the array. Listing III.4a shows a simple function that calls the bsearch() function. This listing borrows the function comp() from Listing III.1, which used qsort(). Listing III.4b shows a binary search, performed without calling bsearch(), for a string in a sorted array of strings. You can make both examples into working programs by combining them with code at the end of this chapter
An example of how to use bsearch().
1: #include <stdlib.h>
2:
3: static int comp(const void *ele1, const void *ele2)
4: {
5: return strcmp(*(const char **) ele1,
6: *(const char **) ele2);
7: }
8:
9: const char *search(const char *key, const char **array,
10: size_t num)
11: {
12: char **p = bsearch(&key, array, num,
13: sizeof(*array), comp);
14: return p ? *p : NULL;
15: }
An implementation of a binary search.
1: #include <stdlib.h>
2:
3: const char *search(const char *key, const char **array,
4: size_t num)
5: {
6: int low = 0;
7: int high = num - 1;
8:
9: while (low <= high)
10: {
11: int mid = (low + high) / 2;
12: int n = strcmp(key, array[mid]);
13:
14: if (n < 0)
15: high = mid - 1;
16: else if (n > 0)
17: low = mid + 1;
18: else
19: return array[mid];
20: }
21: return 0;
22: }
19: return array[mid];
20: }
21: return 0;
22: }
Another simple searching method is a linear search. A linear search is not as fast as bsearch() for searching
among a large number of items, but it is adequate for many purposes. A linear search might be the only method available, if the data isn’t sorted or can’t be accessed randomly. A linear search starts at the beginning
and sequentially compares the key to each element in the data set. Listing III.4c shows a linear search. As with all the examples in this chapter, you can make it into a working program by combining it with code at the
end of the chapter.
among a large number of items, but it is adequate for many purposes. A linear search might be the only method available, if the data isn’t sorted or can’t be accessed randomly. A linear search starts at the beginning
and sequentially compares the key to each element in the data set. Listing III.4c shows a linear search. As with all the examples in this chapter, you can make it into a working program by combining it with code at the
end of the chapter.
An implementation of linear searching.
1: #include <stdlib.h>
2:
3: const char *search(const char *key, const char **array,
4: size_t num)
5: {
6: int i;
7:
8: for (i = 0; i < num; i++)
9: {
10: if (strcmp(key, array[i]) == 0)
11: return array[i];
12: }
13: return 0;
14: }
Cross Reference:
III.5: What is the quickest searching method to use?
III.6: What is hashing?
III.8: How can I search for data in a linked list?
No comments:
Post a Comment