can help me .. i want to create a recursive function for search by binary search method, but i am still confused to thanks .... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

can help me .. i want to create a recursive function for search by binary search method, but i am still confused to thanks ....

c++

9th Jul 2018, 1:06 PM
Rendy Dendimara
Rendy Dendimara - avatar
2 Answers
+ 2
Firstly, understand binary search algo. This algo searches for the element in the sorted array and returns its position if it exists. now, about the function : return type. int, as it return the array index arguements : 1. sorted array(sort the array in ascending order before passing here) 2. start index 3. end index 4. elemnt to be searched Now body of function: BASE CONDITION : if array[(start+end)/2] equals element then return (start+end)/2 i.e. middle element else -> go for recursion with following conditions 1. if start>end -> return -1 i.e element not found 2. if element > array[(start+end)/2] i.e element is greater than the mid element .. call function with second half of aray i.e. start=(start+end)/2+1 and end = end 3. if element< array[(start+end)/2] i.e. element is less than the middle element call the function with first half of array i.e start=start and end = (start+end)/2-1 Try to make function yourself, If I will give you the function directly, then you are at loss, good luck
10th Jul 2018, 12:08 AM
Jain Rishav Amit
Jain Rishav Amit - avatar
+ 2
thank you bro ...
10th Jul 2018, 10:08 AM
Rendy Dendimara
Rendy Dendimara - avatar