Binary search

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 7
Reputation: somnathsarode is an unknown quantity at this point 
Solved Threads: 1
somnathsarode's Avatar
somnathsarode somnathsarode is offline Offline
Newbie Poster

Binary search

 
0
  #1
Dec 28th, 2008
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,056
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 312
ddanbe's Avatar
ddanbe ddanbe is online now Online
Postaholic

Re: Binary search

 
0
  #2
Dec 28th, 2008
At first sight there seems to be nothing wrong with your search.
Is your input sorted? This is rather essential.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: Binary search

 
0
  #3
Dec 28th, 2008
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.
  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
  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 .
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,056
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 312
ddanbe's Avatar
ddanbe ddanbe is online now Online
Postaholic

Re: Binary search

 
0
  #4
Dec 28th, 2008
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: Binary search

 
0
  #5
Dec 28th, 2008
Originally Posted by ddanbe View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,056
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 312
ddanbe's Avatar
ddanbe ddanbe is online now Online
Postaholic

Re: Binary search

 
0
  #6
Dec 28th, 2008
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 7
Reputation: somnathsarode is an unknown quantity at this point 
Solved Threads: 1
somnathsarode's Avatar
somnathsarode somnathsarode is offline Offline
Newbie Poster

Re: Binary search

 
0
  #7
Dec 28th, 2008
hey u have not solve my problem
do u undetstand my problem
its not givinig 1 st locatotion value
when search for frist location
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 7
Reputation: somnathsarode is an unknown quantity at this point 
Solved Threads: 1
somnathsarode's Avatar
somnathsarode somnathsarode is offline Offline
Newbie Poster

Re: Binary search

 
0
  #8
Dec 28th, 2008
yes
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: Binary search

 
0
  #9
Dec 28th, 2008
Excuse me, But could you plz explain your problem more efficiently.
cuz i didn't get your point
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,056
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 312
ddanbe's Avatar
ddanbe ddanbe is online now Online
Postaholic

Re: Binary search

 
0
  #10
Dec 28th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 479 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC