fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int less_int(const void* left, const void* right) {
  5. return *((const int*)left) - *((const int*)right);
  6. }
  7.  
  8. int main(void) {
  9. size_t num_elements = 500;
  10. int* a = malloc(num_elements*sizeof(int));
  11. for(size_t i=0 ; i<num_elements ; i++) {
  12. a[i] = rand() % num_elements;
  13. }
  14. qsort(a, num_elements, sizeof(int), less_int);
  15. size_t num_rand = 100;
  16. int* r = malloc(num_rand*sizeof(int));
  17. for(size_t i=0 ; i < num_rand ; i++) {
  18. r[i] = rand() % num_rand;
  19. }
  20.  
  21. for (size_t i = 0 ; i != num_rand ; i++) {
  22. int *p = (int*) bsearch (&r[i], a, num_elements, sizeof(int), less_int);
  23. if (p) {
  24. printf ("%d is in the array.\n", *p);
  25. } else {
  26. printf ("%d is not in the array.\n", r[i]);
  27. }
  28. }
  29.  
  30. free(a);
  31. free(r);
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0.01s 1808KB
stdin
Standard input is empty
stdout
61 is not in the array.
42 is in the array.
60 is in the array.
17 is not in the array.
23 is not in the array.
61 is not in the array.
81 is in the array.
9 is in the array.
90 is in the array.
25 is not in the array.
96 is not in the array.
67 is in the array.
77 is not in the array.
34 is in the array.
90 is in the array.
26 is in the array.
24 is in the array.
57 is not in the array.
14 is not in the array.
68 is in the array.
5 is in the array.
58 is in the array.
12 is in the array.
86 is in the array.
0 is in the array.
46 is in the array.
26 is in the array.
94 is in the array.
16 is not in the array.
52 is not in the array.
78 is not in the array.
29 is in the array.
46 is in the array.
90 is in the array.
47 is not in the array.
70 is in the array.
51 is in the array.
80 is not in the array.
31 is in the array.
93 is not in the array.
57 is not in the array.
27 is in the array.
12 is in the array.
86 is in the array.
14 is not in the array.
55 is in the array.
12 is in the array.
90 is in the array.
12 is in the array.
79 is in the array.
10 is not in the array.
69 is in the array.
89 is not in the array.
74 is not in the array.
55 is in the array.
41 is in the array.
20 is not in the array.
33 is in the array.
87 is in the array.
88 is not in the array.
38 is in the array.
66 is not in the array.
70 is in the array.
84 is in the array.
56 is in the array.
17 is not in the array.
6 is in the array.
60 is in the array.
49 is in the array.
37 is in the array.
5 is in the array.
59 is in the array.
17 is not in the array.
18 is not in the array.
45 is in the array.
83 is not in the array.
73 is not in the array.
58 is in the array.
73 is not in the array.
37 is in the array.
89 is not in the array.
83 is not in the array.
7 is in the array.
78 is not in the array.
57 is not in the array.
14 is not in the array.
71 is in the array.
29 is in the array.
0 is in the array.
59 is in the array.
18 is not in the array.
38 is in the array.
25 is not in the array.
88 is not in the array.
74 is not in the array.
33 is in the array.
57 is not in the array.
81 is in the array.
93 is not in the array.
58 is in the array.