summaryrefslogtreecommitdiff
path: root/reference/C/EXAMPLES/bsearch.c
blob: 618f114fa8cc8de9f39d217fc04ffd08cd19c6f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/************************************************************************
 *
 * Purpose: To demonstrate the 'bsearch' function.
 *
 * Author:  M.J. Leslie
 *
 * Date:    04-Jun-95
 *
 ************************************************************************/

#include <stdlib.h>

#define NUM 11

static int func (void *, void *);

/************************************************************************/

main()
{
					/* List of numbers.		*/
  int numbers[NUM]={3,4,7,9,11,13,15,17,19,21,23};
  int key=22;				/* number to find.		*/
  int * result;			/* Result of 'bsearch'		*/

					/* Search for 'key' in 'numbers'*/

  result = bsearch (&key, numbers, NUM, sizeof(numbers[0]), (void *)func);

  printf("%d ", key);
  (result) ? puts("found") : puts("not found"); 
}

/************************************************************************/

static int func (void *a, void *b)
{
  printf("%d %d \n", *(int *)a, *(int *)b);	/* Diagnistics.		*/

					/* Compare the two numbers	*/
  if (*(int *)a == *(int *)b) return(0);
  if (*(int *)a <  *(int *)b) return(-1);
  return (1);
}