Thread: Returning a pointer in Binary Search

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Returning a pointer in Binary Search

    I'm almost sure that this does not result in undefined behaviour as what I'm returning is within range of the supplied pointers.
    Still..there may be something I'm not aware of.
    <I'm aware of <algorithm> but this is for a homework >

    Code:
    int* bin_s(int n,int* x, int* y)
    {
        if(x==y)
            if(*x==n)
                return x;
            else return 0;
        else if (y-x==1||x-y==1)
        {
            if(n==*x)return x;
            else if(n==*y)return y;
            else return 0;
        }
        else
        {
            int* temp = x+((y-x)/2);
            int* result;
            if(n>= *temp)
                result = bin_s(n,temp,y);
            else
                result = bin_s(n,x,temp);
            return result;
        }
    
    }
    Does it seem okay?
    Last edited by manasij7479; 09-11-2011 at 10:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-28-2010, 10:35 PM
  2. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  5. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM