Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h> // bsearch()
- int cmp(const void *aptr, const void *bptr)
- {
- int a = *(int *)aptr;
- int b = *(int *)bptr;
- return (a > b) - (a < b);
- }
- void print_arr(char *msg, int *arr, int size)
- {
- printf("%s\n", msg);
- for(int i = 0; i < size; i++)
- printf("%d ", arr[i]);
- printf("\n\n");
- }
- int main(int argc, int *argv[])
- {
- int num[] = {3, -1, 9, 99, 1, 48};
- int key = 9;
- int *ret = bsearch(&key, num, sizeof(num) / sizeof(num[0]), sizeof(num[0]), cmp);
- if(ret != NULL)
- printf("key '%d' found inside num[] array!\n\n", key);
- print_arr("before sort :", num, sizeof(num) / sizeof(num[0]));
- qsort(num, sizeof(num) / sizeof(num[0]), sizeof(num[0]), cmp);
- print_arr("after sort :", num, sizeof(num) / sizeof(num[0]));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement