Link to home
Start Free TrialLog in
Avatar of scottie_24
scottie_24

asked on

Trying to use a sequential search within a binary search

I have a function that isn't giving me the results I need it to. I'm using a binary search to search an array of 1000 elements, and when the size of the search list reduces to less than 15, I need to move to a sequential search. When I comment out the sequential search 'if' statement (starting at the line of ... if ((high2 - low2) < 15){ ...), the binary search works just fine, however when I uncomment it, the function doesn't find the searchKey2 even if it does exist in my array. Here is my function:

int binarySeq(const int c[], int searchKey2, int low2, int high2)
{
     int middle2, count2 = 0;

     while (low2 <= high2){
          middle2 = (low2 + high2) / 2;

        if (searchKey2 == c[middle2]){
             cout << "\n " << count2 + 1 << " comparison(s). ";
             return middle2;
        }

        if ((high2 - low2) < 15){
             for(int n = low2; n < high2; n++){
                  if (c[n] == searchKey2){
            cout << "\n " << count2 + 1  << " comparison(s)." << endl;
            return n;
                  }
                  else{
                      return -1;
                  }
             }
                 }

        else if (searchKey2 < c[middle2])
             high2 = middle2 - 1;
        else
             low2 = middle2 + 1;

        count2++;
     }
     cout << "\n " << count2 << " comparison(s). ";
     return -1;
}

Thank you.
Scottie
Avatar of Tommy Braas
Tommy Braas
Flag of Australia image

Have you tried it this way?:

Have you tried it this way?:

int binarySeq(const int c[], int searchKey2, int low2, int high2)
{
      int middle2, count2 = 0;

      while (low2 <= high2)
      {
            if ((high2 - low2) < 15)
            {
                  for(int n = low2; n < high2; n++)
                  {
                        if (c[n] == searchKey2)
                        {
                              cout << "\n " << count2 + 1  << " comparison(s)." << endl;
                              return n;
                        }
                        else
                        {
                              return -1;
                        }
                  }
            }
            else
            {
                  middle2 = (low2 + high2) / 2;

                  if (searchKey2 == c[middle2])
                  {
                        cout << "\n " << count2 + 1 << " comparison(s). ";
                        return middle2;
                  }
      
                  if (searchKey2 < c[middle2])
                  {
                        high2 = middle2 - 1;
                  }
                  else
                  {
                        low2 = middle2 + 1;
                  }
      
                  count2++;
            }
      }
      cout << "\n " << count2 << " comparison(s). ";
      return -1;
}
Avatar of scottie_24
scottie_24

ASKER

I just tried it, but no luck -- still not finding the element in the array. If this helps any, searchKey2 is the integer I'm searching for, low2 is sent in as 0, and high2 is the size of the array minus 1.
Hi,

At any point in the loop, the key might be found anywhere from c[low2] to c[high2] INCLUSIVE.  That is, the element might be found in c[high2].  The binary search correctly accounts for this - however the sequential search only goes to c[high2 - 1] so you will miss the element if it is the last in the range.

Try changing the line:

for(int n = low2; n < high2; n++)

to

for(int n = low2; n <= high2; n++)
What you're doing is a KLUDGE  of a HIGH ORDER.

If your binary search isnt working, figure out why!

You're probably have an off-by-one error in setting your new limits.

Check the boundary conditions-- assume the item you want is right at your binary probe point,
or at one of the limits.  MAke sure you set the new search limits to include that point.



So I have a system made up of poorly matched components of high order. Thanks ;), I learned a new word today.

The thing I can't understand about my off-by-one error is, if that's the case I would still expect it to match with my search value, but just report it as being in the wrong array position. However, I keep getting that it's not found. When I comment the sequential search out, the binary search works just fine, so I know the parameters of the binarySeq are correct. I believe travd is right with what I need in my for loop, however that's still giving me the same bad results.

I really appreciate the help.

Scottie
One question Scottie, it has to be asked, is your array sorted?
yes it is sorted. here's what my program looks right now:

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <ctime>

using namespace std;

// function prototypes
int binarySearch(const int [], int, int, int);
int binarySeq(const int [], int, int, int);

int main()
{
     int size = 1000;
     int selection = 0;
     int a[size], key, key2, result;
     int hold, i, element;

     for(int p = 0; p < size; p++){
          a[p] = 1 + rand() % 100000;
     }
   
     // sort the array in ascending order
     for(int pass = 0; pass < size - 1; pass++)
          
          for (i = 0; i < size - 1; i++)
            if (a[i] > a[i + 1]){
                 hold = a[i];
                 a[i] = a[i + 1];
                 a[i + 1] = hold;
            }

     system("clear");

     cout << "\n The array has been created...\n";

     do{
        cout << "\n -----------------------------------------------\n"
               << " The range of the random numbers is 1 to 100,000\n"
             << " -----------------------------------------------\n"
             << " 1- Print sorted array\n"
             << " 2- Binary search\n"
             << " 3- Binary with sequential search\n"
             << " 0- Quit\n"
             << " Selection: ";
          cin >> selection;

          if (selection == 1){
             system("clear");
             for(i = 0; i < size; i++){
                cout << a[i] << "\t";
                  if ( i % 9 == 0 && i > 0){
                      cout << endl;
                }
             }
          }

          if (selection == 2){
               cout << " Enter integer to compare> ";
               cin >> key;
               result = binarySearch(a, key, 0, size - 1, size);

             if (result != -1)
               cout << "\n " << key << " found in array element "
                  << result << "." << endl;
              else
               cout << "\n " << key << " not found. " << endl;
          }
 
        if (selection == 3){
             cout << " Enter integer to compare> ";
             cin >> key2;
             element = binarySeq(a, key2, 0, size - 1, size);
            
             if (element != -1)
               cout << "\n " << key2 << " found in array element "
                  << element << "." << endl;
             else
              cout << "\n " << key2 << " not found. " << endl;
        }

     }while(selection != 0);

     // Clear out the array at program close
     size = 0;

     return 0;
}


// Function implementations/functions

int binarySearch(const int b[], int searchKey1, int low, int high)
{
     int middle, count = 0;

     while (low <= high){
          middle = (low + high) / 2;

        if (searchKey1 == b[middle]){
             cout << "\n " << count + 1 << " comparison(s). ";
             return middle;
        }
        else if (searchKey1 < b[middle])
             high = middle - 1;
        else
             low = middle + 1;
        count++;
     }
     cout << "\n " << count << " comparison(s). ";
     return -1;
}

int binarySeq(const int c[], int searchKey2, int low2, int high2)
{
     int middle2, count2 = 0;

     while (low2 <= high2)
     {
          if ((high2 - low2) < 15)
          {
               for(int n = low2; n <= high2; n++)
               {
                    if (c[n] == searchKey2)
                    {
                         cout << "\n " << count2 + 1  << " comparison(s)." << endl;
                         return n;
                    }
                    else
                    {
                         return -1;
                    }
               }
          }
          else
          {
               middle2 = (low2 + high2) / 2;

               if (searchKey2 == c[middle2])
               {
                    cout << "\n " << count2 + 1 << " comparison(s). ";
                    return middle2;
               }
     
               if (searchKey2 < c[middle2])
               {
                    high2 = middle2 - 1;
               }
               else
               {
                    low2 = middle2 + 1;
               }
     
               count2++;
          }
     }
     cout << "\n " << count2 << " comparison(s). ";
     return -1;
}
ASKER CERTIFIED SOLUTION
Avatar of travd
travd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also what kind of compiler do you have?  Because you are calling binarySearch and binarySeq with five arguments - but they only take four!  This should not compile.

The extra "size" argument is not needed - you have had your functions declared in a different way in the past, but you only need the array, the key, the high and low indices, such as:

element = binarySeq(a, key2, 0, size - 1);

Hope this helps!
... the above should read "...you MAY have had your functions..."
Thank you very much. Yes, I noticed that parameter mistake -- I notice it before I copied and pasted my code, but didn't fix all the function calls obviously. I using Linux to compile.

As soon as I figure out how to divie up points, I be sending them out.

Thank you again, especially travd and orangehead.

Scottie
You're welcome. I tried to compile the code on my system and managed to find a severe bug in gcc 3.3, which is why I've been a bit quite lately.

Good luck!