Thread: Recursive Binary Search

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    Recursive Binary Search

    Hey, guys. I'm having trouble with creating a code for a recursive binary search. Whenever I run the code, it says "Segmentation fault (core dumped)". What should I change in my code? Here it is:

    Code:
    while(lower<=upper){
        input2(&search);
        if(search==-1)
            printf("break;");
        printf("Found %d at index %d", search, binary(array, n, search, lower, upper));
        }
    
    int binary(int array, int n, int search, int lower, int upper){
    
    
    int middle;
    lower=0;
    upper=n-1;
    
    
    
    
        middle=(lower+upper)/2;
    
    
          if (array[middle] > search) // key is in lower subset
            return binary(array, n, search, lower, middle-1);
          else if (array[middle] < search) // key is in upper subset
            return binary(array, n, search, middle+1, upper);
          else // key has been found
            return middle; 
    }
    Last edited by Tim Arevalo; 07-29-2012 at 11:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive binary search program help.
    By mooney in forum C Programming
    Replies: 3
    Last Post: 04-04-2008, 01:12 AM
  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. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM
  4. recursive search
    By bhorrobi in forum Windows Programming
    Replies: 2
    Last Post: 10-03-2003, 03:41 AM
  5. recursive binary search
    By condorx in forum C Programming
    Replies: 3
    Last Post: 12-03-2002, 12:31 PM