954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Binary search to find upper and lower bound values

Hi folks,
I did a program to find a number at a lower bound position and upper bound position. For example,
this is my array values
1 1 2 3 5 5 5 5 7 8 9 10
if i search lower position for 5 , it will return 4 . and for upper bound it will return 7.

Actually i did a two function to find the numbers where it is locating for lower and upper bound.

int binarylower(int a[],int m,int l,int u)
{
	int mid,c=0;
   	if(l<=u)
	{
 	    	mid=(l+u)/2;
      		if(m==a[mid])
      		{
			c=m;
        	 	return binarylower(a,m,l,mid-1);
      		}
      		else if(m<a[mid])
		{	
        	  	return binarylower(a,m,l,mid-1);
      		}
      		else
		{     
			return binarylower(a,m,mid+1,u);
       		}
    	}

   	else
        	return l;
  }

int binaryupper(int a[],int m,int l,int u)
{
	int mid,c=0;
   	if(l<=u)
	{
 	    	mid=(l+u)/2;
      		if(m==a[mid])
      		{
			c=m;
        	 	return binaryupper(a,m,mid+1,u);
      		}
      		else if(m<a[mid])
		{	
        	  	return binaryupper(a,m,l,mid-1);
      		}
      		else
		{     
			return binaryupper(a,m,mid+1,u);
       		}
    	}

   	else
        	return u;
  }


Now i need only one function to find the lower and upper position. so i added extra parameter called 'mode' in that function. if mode value is 0, it will return lower position otherwise it will return upper position. This is my program

int binary(int a[],int m,int l,int u,int uorl)
{
	int mid,c=0;
   	if(l<=u)
	{
 	    	mid=(l+u)/2;
      		if(m==a[mid])
      		{
			c=m;
			if (uorl==0)
        	 		return binary(a,m,l,mid-1,0);
			else
				return binary(a,m,mid+1,u,1);
      		}
      		else if(m<a[mid])
		{	
        	  	return binary(a,m,l,mid-1,0);
      		}
      		else
		{     
			return binary(a,m,mid+1,u,0);
       		}
    	}

   	else
	{
		if (uorl==0)
		
        		return l;
		else
			return u;
	}
  }


But i couldn't get the correct answer from the above function. If any one knows the solution please help me

infantheartlyje
Light Poster
29 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

On line 17 and 21 you are passing 0 for the uorl value all the time. You need to add an if else just like the others

// Code starting at line 15 in binary function,
// Fix it in both else ifs
else if(m<a[mid])
{
    if (uorl==0)
        return binary(a,m,l,mid-1,0);
     else
        return binary(a,m,l,mid-1,1);
}

Do both else ifs.

histrungalot
Posting Whiz in Training
266 posts since May 2008
Reputation Points: 76
Solved Threads: 34
 

Wasn't thinking. Don't pass a hardcode value as the argumnet of to binary, pass uorl instead.

binary(a,m,l,mid-1,uorl);
histrungalot
Posting Whiz in Training
266 posts since May 2008
Reputation Points: 76
Solved Threads: 34
 

On line 17 and 21 you are passing 0 for the uorl value all the time. You need to add an if else just like the others

// Code starting at line 15 in binary function,
// Fix it in both else ifs
else if(m<a[mid])
{
    if (uorl==0)
        return binary(a,m,l,mid-1,0);
     else
        return binary(a,m,l,mid-1,1);
}

Do both else ifs.

Thank You for your reply. And i tried like this. but its not working.

if(m==a[mid])
      		{
			c=m;
			if (uorl==0)
        	 		return binary(a,m,l,mid-1,0);
			else
				return binary(a,m,mid+1,u,1);
      		}
      		else if(m<a[mid])
		{	
        	  	if (uorl==0)
				return binary(a,m,l,mid-1,0);
			else
				return binary(a,m,mid+1,u,1);
      		}
      		else
		{     
			if (uorl==0)
				return binary(a,m,l,mid-1,0);
			else
				return binary(a,m,mid+1,u,1);
       		}
infantheartlyje
Light Poster
29 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Check you mid+1 and mid-1 combinations. It looks wrong. Upper is +1, -1, +1 and lower is -1, -1, +1.

histrungalot
Posting Whiz in Training
266 posts since May 2008
Reputation Points: 76
Solved Threads: 34
 
Check you mid+1 and mid-1 combinations. It looks wrong. Upper is +1, -1, +1 and lower is -1, -1, +1.


Now i changed my code like this.

int binary(int a[],int m,int l,int u,int uorl)
{
	int mid,c=0;
   	if(l<=u)
	{
 	    	mid=(l+u)/2;
      		if(m==a[mid])
      		{
			c=m;
			if (uorl==0)
        	 		return binary(a,m,l,mid-1,0);
			else
				return binary(a,m,mid+1,u,1);
      		}
      		else if(m<a[mid])
		{	
        	  	if (uorl==0)
				return binary(a,m,l,mid,0);
			else
				return binary(a,m,l,mid-1,1);
      		}
      		else
		{     
			if (uorl==0)
				return binary(a,m,mid+1,u,0);
			else
				return binary(a,m,mid,u,1);
       		}
    	}

   	else
	{
		if (uorl==0)
		
        		return l;
		else
			return u;
	}
  }


Now if my array value like this,
1 2 3 4 5 5 5 5 6 6 6 6 7 9 9

1. if i search 8 , means its giving segmentation fault. if it is not found means, i need position of number 7.

2. if i search 1(first number) means, its resulting 1 is not found at 0th position for both lower and upper case.

3. if search 9 (last number) means, if there is two 9 , it resulting correctly. It there is only one 9 means, it resulting correctly for lower position. for upper position search its resulting segmentation fault.

Why it is :-(

infantheartlyje
Light Poster
29 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

As I posted elsewhere
Try printing out a copy of your program, grab some paper and a pencil. Sit down at a desk or a table.

Start at the first executable statement in main() and follow the program statement by statement. Write down the values of all variables. Write down everything that is displayed. IOW,you be the computer and desk-check your program.

This is a fundamental part of programming -- checking your work.
Do the same with the algorithm you are trying to implement and note where your program and the algorithm seem to coincide -- and there they diverge.

All you seem to be doing is throwing new changes around without understanding what those changes really do. In fact you probably don't quite understand what the program is really doing at the moment (I know this because if you did know it would be obvious to you what to change).

Use the technique quoted above to understand what you wrote. And that will tell you what you need to fix.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

There is a lot going on. The boundaries are the issue. See if this helps.

#include <stdlib.h>
#include <stdio.h>

int binary(int a[],int size,int m,int l,int u,int uorl) {
   int mid,ret;
   if(l<=u ) {
      mid=(l+u)/2;
      if(m==a[mid]) {
         if (uorl==0){
            ret = binary(a,size,m,l,mid-1,uorl);
         }
         else{
            ret = binary(a,size,m,mid+1,u,uorl);
         }
      }
      else if(m<a[mid]) {	
         ret = binary(a,size,m,l,mid-1,uorl);
      }
      else {     
         ret = binary(a,size,m,mid+1,u,uorl);
      }
   }
   else {
      if (uorl==0){
         // Check boundaries
         if ( l > size || (l > u && m !=a[l] && u > 0)) 
            l--;  // Fix boundaries
         ret = l;
      }
      else{
         // Check boundaries
         if ( l < u || u < 0)
            u++; // Fix boundaries
         ret =  u;
      }
   }
   return ret;
}

int main(int argc, char **argv){
  int a[15] =  {1,2,3,4,5,5,5,5,6,6,6,6,7,9,9};
  //  int a[5] =  {1,3,5,5,6};
  //  int a[2] =  {1,2};
  int i,val;
  int upper=sizeof(a)/sizeof(int);
  if ( argc != 2 ){
     printf("Usage: %s <num to find>\n",argv[0]);
     return -1;
  }
  val = atoi(argv[1]); 
  printf("Look for: %d\n",val);
  printf("Lower: %d\n",binary(a,upper-1,val,0,upper-1,0));
  printf("Upper: %d\n",binary(a,upper-1,val,0,upper-1,1));
  return 0;
}
histrungalot
Posting Whiz in Training
266 posts since May 2008
Reputation Points: 76
Solved Threads: 34
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: