+ 2

Can anyone tell me the short code for performing binary search program

16th Dec 2016, 11:38 AM
Saksham Jain
Saksham Jain - avatar
5 Respostas
+ 1
create a recursive function... store the length of your array or list in a variable (n)... divide it by 2..... now get your input number to be searched as (x).... compare x with (n/2 + 1)th element... if x is lesser than it, give a recursive call for search function in 1st half i.e. in 1 to (n/2) elements.. else if x is greater than it, give a recursive call for search in the 2nd half of array i.e in (n/2 + 1) to n elements..... your terminatiin condition will be when u find a match to (x)...... :)
16th Dec 2016, 1:59 PM
Apurva Tripathi
Apurva Tripathi - avatar
+ 1
#include <stdlib.h> typedef int (*comparator) (const void* a, const void* b); void* bsearch(void* key, void* array, int count, int size_of_each_element, comparator comp);
30th Dec 2016, 6:41 AM
Suvaditya
Suvaditya - avatar
0
thanks for the answer,but it would be a favour if u please write the program
16th Dec 2016, 2:49 PM
Saksham Jain
Saksham Jain - avatar
0
I will try to get back to you with a program.... :)
16th Dec 2016, 2:51 PM
Apurva Tripathi
Apurva Tripathi - avatar
0
thanks suvanditya
30th Dec 2016, 6:42 AM
Saksham Jain
Saksham Jain - avatar