Understanding binsearch

HI everyone, im new to learning C++ language and i would just like to ask how does the binsearch function work, what parameters should i feed into it and an example of input to understand how the whole function works. The binsearch function is as below. Thank you in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int binsearch (char *word, struct key tab[], int n)
 { 
           int cond;
           int low, high, mid;
           low = 0;
           high = n - 1; 
           while (low <= high) {     //Decision 1 
                   mid = (low+high) / 2;
                   if ((cond = strcmp(word, tab[mid].word)) < 0) //Decision 2 
                          high = mid - 1;
                   else if (cond > 0) //Decision 3
                          low = mid + 1; 
                   else 
                           return mid; 
             }
                return -1; 
   }            
how does the binsearch function work
http://en.wikipedia.org/wiki/Binary_search_algorithm

what parameters should i feed into it
word is a c-string containing the word you are looking for.

tab[] is a array of key structures. It should be sorted by .word members.

n is the size of array.

an example of input to understand how the whole function works.
If you provide key definition, sure.

im new to learning C++
And the code you posted is fairly old C actually.
Topic archived. No new replies allowed.