bsearch man page on HP-UX

Man page or keyword search:  
man Server   10987 pages
apropos Keyword Search (all sections)
Output format
HP-UX logo
[printable version]

bsearch(3C)							   bsearch(3C)

       bsearch() - binary search a sorted table

       is  a binary search routine generalized from Knuth (6.2.1) Algorithm B.
       It returns a pointer into a table  indicating  where  a	datum  may  be
       found.  The table must be previously sorted in increasing order accord‐
       ing to a provided comparison function.  key points to a datum  instance
       to  be  sought in the table.  base points to the element at the base of
       the table.  nel is the number of elements in the table.	 size  is  the
       size  of each element in the table.  compar is the name of the compari‐
       son function, which is called with two arguments that point to the ele‐
       ments  being  compared.	The function must return an integer less than,
       equal to, or greater than zero indicating that the first argument  (the
       key)  is to be considered less than, equal to, or greater than the sec‐
       ond argument (the array element).

       The pointers to the key and the element at the base of the table should
       be of type pointer-to-element, and cast to type pointer-to-void.

       The  comparison function need not compare every byte, so arbitrary data
       can be contained in the elements in addition to the values  being  com‐

       Although declared as type pointer-to-void, the value returned should be
       cast into type pointer-to-element.

       A NULL pointer is returned if the key cannot be found in the table.

       The example below searches a table containing pointers  to  nodes  con‐
       sisting	of  a  string and its length.  The table is ordered alphabeti‐
       cally on the string in the node pointed to by each entry.

       This code fragment reads in strings and either finds the	 corresponding
       node  and prints out the string and its length, or prints an error mes‐

	      #include <stdio.h>
	      #include <stdlib.h>
	      #include <string.h>

	      #define TABSIZE  1000

	      struct node {		      /* these are stored in the table */
		  char *string;
		  int length;
	      struct node table[TABSIZE];     /* table to be searched */
		  struct node *node_ptr, node;
		  /* routine to compare 2 nodes */
		  int node_compare(const void *, const void *);
		  char str_space[20];	     /* space to read string into */
		  node.string = str_space;
		  while (scanf("%s", node.string) != EOF) {
		      node_ptr = (struct node *)bsearch((void *)(&node),
			     (void *)table, TABSIZE,
			     sizeof(struct node), node_compare);
		      if (node_ptr != NULL) {
			  (void)printf("string = %20s, length = %d\n",
			      node_ptr->string, node_ptr->length);
		      } else {
			  (void)printf("not found: %s\n", node.string);

	      /*      This routine compares two nodes based on an
		      alphabetical ordering of the string field.
	      node_compare(const void *node1, const void *node2)
	      struct node *node1, *node2;
		  return strcoll(((const struct node *)node1)->string,
			      ((const struct node *)node2)->string);

       If the table being searched contains two or more entries that match the
       selection  criteria, a random entry is returned by as determined by the
       search algorithm.

       hsearch(3C), lsearch(3C), qsort(3C), tsearch(3C), thread_safety(5).


List of man pages available for HP-UX

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
Vote for polarhome
Free Shell Accounts :: the biggest list on the net