Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <search.h>
- void lsearch_demo();
- void lfind_demo();
- void bsearch_demo();
- void qsort_demo();
- int values[] = { 10, 40, 100, 20, 90, 25, 0 };
- int compare(const void * a, const void * b)
- {
- return (*(int*)a - *(int*)b);
- }
- int _tmain(void)
- {
- lfind_demo();
- lsearch_demo();
- bsearch_demo();
- return 0;
- }
- void lsearch_demo()
- {
- int * pItem;
- int key = 140;
- unsigned int num = 6;
- pItem = (int*)_lsearch(&key, values, &num, sizeof(int), compare);
- if (pItem != NULL)
- printf("%d is in the array\n", *pItem);
- else
- printf("%d is not in the array\n", key);
- }
- void lfind_demo()
- {
- int * pItem;
- int key = 140;
- unsigned int num = 6;
- pItem = (int*)_lfind(&key, values, &num, sizeof(int), compare);
- if (pItem != NULL)
- printf("%d is in the array\n", *pItem);
- else
- printf("%d is not in the array\n", key);
- }
- void bsearch_demo()
- {
- int * pItem;
- int key = 40;
- unsigned int num = 6;
- pItem = (int*)bsearch(&key, values, num, sizeof(int), compare);
- if (pItem != NULL)
- printf("%d is in the array\n", *pItem);
- else
- printf("%d is not in the array\n", key);
- }
- void qsort_demo()
- {
- int num = 6;
- qsort(values, num, sizeof(int), compare);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement