Binary Search in C help

This is a discussion on Binary Search in C help within the C Programming forums, part of the General Programming Boards category; Hi...i am trying to write a program that allows me to locate a number in an array with fifteen numbers ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    1

    Unhappy Binary Search in C help

    Hi...i am trying to write a program that allows me to locate a number in an array with fifteen numbers in it. I have been working on this code for a couple of days now and still can't get it to work. i have sorted the array already but now i am trying to do what is called a binary search to locate a number within the array. For example...If i input the number 28 then the output should say 28 found at position 8 or wherever. 28 is located within the array. if the number is not found then it is supposed to say 28 not found. Below is the code that i have for the shell sort and search. I have the Shell sort working so the part that i am trying to get working is the part after Binary Search. Any help would be much appreciated. I am a beginer so be simple please... Thanks so much!

    void main()
    {
    int x[] = {28, 35, 16, 42, 92, 57, 81, 6, 10, 72, 38, 52, 32, 16, 7};
    int temp, i, j, notfound,jump, first,last,mid, end;
    char more;

    clrscr();
    printf("\n\n\tUnsorted Seq: ");
    for (i = 0; i < 15; i++)
    printf ("%d,", x[i]);

    // Shell Sort

    for(jump=15/2;jump>0;jump=jump/2)
    {
    for (i = jump; i < 15; ++i)
    {
    for(j=i-jump; j>=0 && x[j] > x[j + jump]; j=j-jump)
    {
    temp = x[j];
    x[j] = x[j + jump];
    x[j + jump] = temp;
    }
    }
    }
    printf("\n\t Sorted Seq: ");
    for (i = 0; i < 15; i++)
    printf ("%d,", x[i]);

    // Binary Search (the part that does'nt work)
    do
    {
    printf ("\n\n\t\tPlease input a number: ");
    scanf ("%d", &temp);

    first = 0;
    last = 14;
    mid = (last +first)/2;

    while(first<=last)

    {

    if(temp >x[mid])
    first = mid +1;
    else if(temp< x[mid])
    last = mid -1;
    if(temp==x[mid])
    {
    printf ("\n\t\t%d is found at postion %d", temp,i);
    break;
    }
    }
    if (first>last)
    printf ("\n\t\t%d is not found", temp);
    printf ("\n\t\tDo more (Y/N)? ");
    scanf ("%s", &more);
    }
    while (more == 'Y' || more == 'y');
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *Reels in Horron* My god, man! Use code tags!

    Code:
    loop:
        middlevalue = (last+first)/2;
        if( array[middlevalue] == tofind )
            break;
        else
        if( last == first )
            done_without_finding_anything
    
        if( array[middlevalue] < tofind )
            last = middlevalue;
        else
            first = middle value;
    That's all there is to it.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    There is also a standard library function that does a binary search for you:
    Code:
    #include <stdlib.h>
    void *bsearch(const void *key, const void *buf, size_t num, size_t size, int (*compare)(const void *, const void *));
    
    // http://www.cplusplus.com/ref/cstdlib/bsearch.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  2. deleting all nodes of a binary search tree...
    By sachitha in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2004, 07:19 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21