Thread: Binary Search Help

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Binary Search Help

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    main()
    {
    int count,count2,count3,count4,array[50],temp,search,high=49,low=0,location=25;
    srand(2);
    for(count=0;count<50;count++)
        {
        array[count]=rand()%1000;
        }
    for(count3=0;count3<count;count3++)
        {
        for(count2=0;count2<count-1;count2++)
            {
            if(array[count2]>array[count2+1])
                {
                temp=array[count2];
                array[count2]=array[count2+1];
                array[count2+1]=temp;
                }
            }
        }
    for(count2=0;count2<count;count2++)
        printf("%i\n",array[count2]);
    printf("Enter a number and I will tell you it's location.");
    scanf("%i", &search);
    while(search!=array[location]||(low-high)==1)
        {
        if(array[location]==search)
            printf("%i was located in location %i\n",search,location);
        if(array[location]!=search)
            printf("Between %i and %i\n",low,high);
        if(array[location]<search)
            {
            low=location;
            location=(low+high)/2;
            }
        else
            {
            high=location;
            location=(low+high)/2;
            }
        }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Learn what a comment is.
    Add them to your program.

    Learn to use constants either MACRO or real constants. Do not use variable where it should be an constant.

    Code:
    for(count3=0;count3<count;count3++)
    Learn to ask a specific question.

    Tim S.
    Last edited by stahta01; 11-14-2011 at 07:35 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Get a good binary search:
    Code:
    int binarySearch(int array[], int goal, int left, int right) {
       int lo, mid, hi;
       lo=left;
       hi=right;
       while (lo <= hi) {
          mid = (lo + hi) / 2;
          if(goal < array[mid])
             hi = mid-1;
          else if(goal > array[mid])  
             lo = mid+1;
          else
             return mid;
       }
       return -1;
    }
    If the return value from the binary search function is >-1, then it will return the index number to the goal, in the array[]. Otherwise, the value was not found.

    If the array being searched is not sorted, the function may fail.
    Last edited by Adak; 11-14-2011 at 08:30 PM.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Lightbulb Hello

    Quote Originally Posted by stahta01 View Post
    Learn what a comment is.
    Add them to your program.

    Learn to use constants either MACRO or real constants. Do not use variable where it should be an constant.

    Code:
    for(count3=0;count3<count;count3++)
    Learn to ask a specific question.

    Tim S.
    Learn what a jerk is.
    You are one.

    This is my first time on here asking you "experts" a question.
    Sorry for not knowing how to code like a master, and not knowing everything about C.
    BTW, My problem with the code was that in the binary search is that it was not running properly every time I entered a number, it would enter into an infinite loop..But I managed to fix my problem... Sorry for the troubles.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    Thanks for the help adak!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Zach, Welcome to the forum!

    When you come to a programming forum, like many other kinds of fourms, people are unusually curt - that's just the nature of impersonal communication, and limited time - and no way can the keyboard keep up with our thoughts we want to express.

    Don't let anything get personal with you, and please, don't get into name calling, flame wars, etc. Try hard to let it all roll off you, like water off a duck's back. You can't do it all the time, (me neither), but it helps a lot to try.

    And you're welcome.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    FYI, posting code without any accompanying explanation of the problem or asking a question is like walking up to a colleague's desk, slamming a piece of paper with code and your thread title at the top on it, down in front of them and then immediately briskly walking away.
    You need to have courtesy to politely ask for help with a specific problem or you will get an appropriately blunt response at best.

    That said...
    What your while loop does is continue looping while either of these things are true:
    Code:
    search!=array[location]
    (low-high)==1)
    Do you really want it to keep going if that last part were true? Also, when would it even become true?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Learn to Google on how to ask a smart question.

    So, helping you is being a jerk.

    How To Ask Questions The Smart Way

    Tim S.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your reply was good Tim. New members have a hard time dealing with "concentrated" replies, and think it's personal. In R/L it would be impolite, of course, but here, we need to concentrate our communication a bit to offset the slower throughput of a forum.

    You were being helpful, but he didn't know it.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    Sorry about that, I just dident know about the fourm form.. SEE WHAT I DID THERE!! ANYHOO... My apologies to Tim, and thanks for the help to a new member... I managed to get the code to work after a few hours of tinkering with it...

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Zachery Montoya View Post
    Sorry about that, I just dident know about the fourm form.. SEE WHAT I DID THERE!! ANYHOO... My apologies to Tim, and thanks for the help to a new member... I managed to get the code to work after a few hours of tinkering with it...
    Something to keep in mind when dealing with us coder types is that we work with very precise grammar and spelling day in and day out. After a while it does affect the way we communicate... we become very direct and often blunt but most often no offense is intended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 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

Tags for this Thread