943,700 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 694
  • C++ RSS
Dec 28th, 2008
0

Binary search

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. // Program for Binary search
  2. #include<stdio.h>
  3. #include<conio.h>
  4. int a[50];
  5. void main()
  6. {
  7. int ch1=0,n=0,i;
  8. clrscr();
  9. while(ch1!=4)
  10. {
  11. printf("\n 1 : Read data ");
  12. printf("\n 2 : Search data ");
  13. printf("\n 3 : Show data ");
  14. printf("\n 4 : Exit ..... ");
  15. printf("\n Enter your choice: ");
  16. scanf("%d",&ch1);
  17. switch(ch1)
  18. {
  19. case 1:
  20. printf("\n Enter no of values to read : ");
  21. scanf("%d",&n);
  22. for(i=0;i<n;i++)
  23. scanf("%d",&a[i]);
  24. break;
  25. case 2:
  26. printf("\n Enter valu to be search : ");
  27. scanf("%d",&i);
  28. i=bsearch(a,0,n-1,i);
  29. if(i>0)
  30. printf("\n Value is found at index %d ",i);
  31. else
  32. printf("\n Value is not found ");
  33. break;
  34. case 3:
  35. for(i=0;i<n;i++)
  36. printf("\n %5d",a[i]);
  37. break;
  38.  
  39. }
  40. }
  41. }
  42.  
  43. int bsearch(int a[],int lb,int ub,int target)
  44. {
  45. int mid;
  46. if(lb<=ub)
  47. {
  48. mid=(lb+ub)/2;
  49. if(a[mid]==target)
  50. return mid;
  51. else if(a[mid]>target)
  52. bsearch(a,lb,mid-1,target);
  53. else
  54. bsearch(a,mid+1,ub,target);
  55.  
  56. }
  57. else
  58. return -1;
  59.  
  60. }
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
somnathsarode is offline Offline
7 posts
since Dec 2008
Dec 28th, 2008
0

Re: Binary search

At first sight there seems to be nothing wrong with your search.
Is your input sorted? This is rather essential.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Dec 28th, 2008
0

Re: Binary search

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.
Cpp Syntax (Toggle Plain Text)
  1. int bsearch(int a[],int lb,int ub,int target)
  2. {
  3. int result; //Here is the variable that accepts the result of the backtracking
  4. int mid;
  5. if(lb<=ub)
  6. {
  7. mid=(lb+ub)/2;
  8. if(a[mid]==target)
  9. return mid;
  10. else if(a[mid]>target)
  11. result=bsearch(a,lb,mid-1,target);
  12. else
  13. result=bsearch(a,mid+1,ub,target);
  14.  
  15. }
  16. else
  17. return -1;
  18.  
  19. return result;
  20. }

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)
  1. case 2:
  2. {
  3. printf("\n Enter valu to be search : ");
  4. scanf("%d",&i);
  5. i=bsearch(a,0,n-1,i);
  6. if(i>=0) //Here is the correction just add the "=" sign
  7. printf("\n Value is found at index %d ",i);
  8. else
  9. printf("\n Value is not found ");
  10. break;
  11. }

Thirdly:-
MAKE SURE that the values stored in the array is SORTED .


I'll be happy of Any critique .
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
Dec 28th, 2008
0

Re: Binary search

Quote originally posted by Ahmed_l ...
result=bsearch(a,lb,mid-1,target);
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.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Dec 28th, 2008
0

Re: Binary search

Click to Expand / Collapse  Quote originally posted by ddanbe ...
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.
Mr ddanbe,
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.
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
Dec 28th, 2008
0

Re: Binary search

Quote originally posted by Ahmed_l ...
this is a small program so it doesn't matter
I was not making any criticism here. If you want to use a variable, please do. Nobody will get hurt.
But in programming you must always be aware of optimisation, so if you can, do it.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Dec 28th, 2008
0

Re: Binary search

hey u have not solve my problem
do u undetstand my problem
its not givinig 1 st locatotion value
when search for frist location
Reputation Points: 10
Solved Threads: 1
Newbie Poster
somnathsarode is offline Offline
7 posts
since Dec 2008
Dec 28th, 2008
0

Re: Binary search

yes
Reputation Points: 10
Solved Threads: 1
Newbie Poster
somnathsarode is offline Offline
7 posts
since Dec 2008
Dec 28th, 2008
0

Re: Binary search

Excuse me, But could you plz explain your problem more efficiently.
cuz i didn't get your point
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
Dec 28th, 2008
0

Re: Binary search

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.
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Invoking a button_click through a function
Next Thread in C++ Forum Timeline: how to save big number into array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC