| | |
Binary search
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hey i have created binary search prograram
this progaram search all locations but not first
if i give value of other than zero location it gives results
but not for zero location
please guid me
the code is as follows
this progaram search all locations but not first
if i give value of other than zero location it gives results
but not for zero location
please guid me
the code is as follows
C++ Syntax (Toggle Plain Text)
// Program for Binary search #include<stdio.h> #include<conio.h> int a[50]; void main() { int ch1=0,n=0,i; clrscr(); while(ch1!=4) { printf("\n 1 : Read data "); printf("\n 2 : Search data "); printf("\n 3 : Show data "); printf("\n 4 : Exit ..... "); printf("\n Enter your choice: "); scanf("%d",&ch1); switch(ch1) { case 1: printf("\n Enter no of values to read : "); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); break; case 2: printf("\n Enter valu to be search : "); scanf("%d",&i); i=bsearch(a,0,n-1,i); if(i>0) printf("\n Value is found at index %d ",i); else printf("\n Value is not found "); break; case 3: for(i=0;i<n;i++) printf("\n %5d",a[i]); break; } } } int bsearch(int a[],int lb,int ub,int target) { int mid; if(lb<=ub) { mid=(lb+ub)/2; if(a[mid]==target) return mid; else if(a[mid]>target) bsearch(a,lb,mid-1,target); else bsearch(a,mid+1,ub,target); } else return -1; }
Last edited by peter_budo; Dec 28th, 2008 at 5:33 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
•
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
Your Binary Search Function's logic is right but You have some simple errors :-
Firstly in the function since u intending using Recursion then u have to return the value that is resulted from the backtracking to a Variable and return this Variable to the Main function.
Secondly:-
In the main function in case '2' when the function returns the index of the first value of the array to variable i the index that will be returned is 0
and since u made a check if i>0 only it will print "Value is not found"
here is the correction
Thirdly:-
MAKE SURE that the values stored in the array is SORTED .
I'll be happy of Any critique .
Firstly in the function since u intending using Recursion then u have to return the value that is resulted from the backtracking to a Variable and return this Variable to the Main function.
Cpp Syntax (Toggle Plain Text)
int bsearch(int a[],int lb,int ub,int target) { int result; //Here is the variable that accepts the result of the backtracking int mid; if(lb<=ub) { mid=(lb+ub)/2; if(a[mid]==target) return mid; else if(a[mid]>target) result=bsearch(a,lb,mid-1,target); else result=bsearch(a,mid+1,ub,target); } else return -1; return result; }
Secondly:-
In the main function in case '2' when the function returns the index of the first value of the array to variable i the index that will be returned is 0
and since u made a check if i>0 only it will print "Value is not found"
here is the correction
Cpp Syntax (Toggle Plain Text)
case 2: { printf("\n Enter valu to be search : "); scanf("%d",&i); i=bsearch(a,0,n-1,i); if(i>=0) //Here is the correction just add the "=" sign printf("\n Value is found at index %d ",i); else printf("\n Value is not found "); break; }
Thirdly:-
MAKE SURE that the values stored in the array is SORTED .
I'll be happy of Any critique .
•
•
•
•
Originally Posted by Ahmed_l
result=bsearch(a,lb,mid-1,target);
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Nov 2008
Posts: 32
Reputation:
Solved Threads: 1
•
•
•
•
Quite right Ahmed_l, I overlooked it! But it is easier to say : return bsearch(a,lb,mid-1,target); that way you don't need the extra result variable.
It is only matter of style not more and this is a small program so it doesn't matter if i added another variable, ofcourse u right and thanks for this advice.
Last edited by Ahmed_I; Dec 28th, 2008 at 7:56 am.
•
•
•
•
Originally Posted by Ahmed_l
this is a small program so it doesn't matter
But in programming you must always be aware of optimisation, so if you can, do it.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
If you are searching for the number 3(target) and if a[0) contains 3 you will get the index which is 0 you will not get the value which is 3, but that was the value you where searching in the array a. So 0 means you found the value 3 in the array, otherwise if the value 3 was not in your array you would have gotten the value -1.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- binary search (C)
- searching and inserting node in a binary search tree (C)
- recursive findaverage function for Binary search tree (C)
- Binary search tree removal (C++)
- binary search (C++)
- Insertion in a binary search tree (C++)
Other Threads in the C++ Forum
- Previous Thread: Invoking a button_click through a function
- Next Thread: how to save big number into array
Views: 479 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






