Recursive binary search

Reply

Join Date: Sep 2004
Posts: 1
Reputation: Sushi is an unknown quantity at this point 
Solved Threads: 0
Sushi Sushi is offline Offline
Newbie Poster

Recursive binary search

 
0
  #1
Sep 22nd, 2004
I'm trying to write a function that gets an array, it's size and the number I'm looking for. The function should be recursive.
That's what I've written, it doesn't work. I don' understand why. Suggestions are welcome.
  1. #include <stdio.h>
  2. int binary (int *a, int n, int num)
  3. {
  4. int temp = n/2;
  5. int c = -1;
  6. if (n==1)
  7. {
  8. if (*a == num)
  9. return (*a-1);
  10. else return c;
  11. }
  12.  
  13. if (a[temp] < num)
  14. return binary ((a+temp+1), (temp-1), num);
  15. else return binary (a, (temp+1), num);
  16.  
  17. return (a + temp) ;
  18. }
  19.  
  20. int main (void)
  21. {
  22.  
  23. int a[5] = {1, 2, 3, 4, 5};
  24. int n = 5, num =0;
  25. printf ("%d", binary (a, n, num));
  26. return 0;
  27. }
Plus, I'm looking for programs that use backtracking, or some explanation on te subject of backtracking. Anyone know any helpful sites?
Thanks, S.u.s.h.i.
Last edited by Narue; Apr 23rd, 2009 at 4:53 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Recursive binary search

 
0
  #2
Sep 22nd, 2004
Why does it need to be recursive? Just because that's the assignment? Non-recursive binary searches are one of life's simple beauties.

In this case, stepping through a debugger would be helpful, but you don't return the value when its found because you test for < num but not == num. that is, you say if a[temp] < num binary(upper half) else binary(lower half); you forgot if a[temp] == num return temp.

More than that, though is the question "what does the routine return" I would think you want to return the INDEX of where it is found; it looks like you are returning the contents sometimes.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 1
Reputation: tyameh is an unknown quantity at this point 
Solved Threads: 0
tyameh tyameh is offline Offline
Newbie Poster

A C program for Recursive binary search

 
-2
  #3
Apr 23rd, 2009
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a[10],i,item;
  6. void binary(int x[10],int beg,int end,int y);
  7. clrscr();
  8. for(i=0;i<10;i++)
  9. {
  10. printf("enter no.:");
  11. scanf("%d",&a[i]);
  12. }
  13. printf("enter no. to be found:");
  14. scanf("%d",&item);
  15. binary(a,0,9,item);
  16. getch();
  17. }
  18. void binary(int x[10],int beg,int end,int y)
  19. {
  20. int mid,z;
  21. if(beg<=end)
  22. {
  23. mid=(beg+end)/2;
  24. if(x[mid]==y)
  25. {
  26. printf("number found");
  27. }
  28. if(y<x[mid])
  29. {
  30. end=mid-1;
  31. binary(x,beg,end,y);
  32. }
  33. else
  34. {
  35. beg=mid+1;
  36. binary(x,beg,end,y);
  37. }
  38. }
  39. }
Last edited by Narue; Apr 23rd, 2009 at 4:53 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Recursive binary search

 
0
  #4
Apr 23rd, 2009
When somebody bumps a thread that hasn't been touched in years to post code, it's pretty much always crap riddled with poor practices. Coincidence? Probably not.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC