# 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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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;
}
Author Commented:
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.
Commented:
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++)
Commented:
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.

Author Commented:
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
Commented:
One question Scottie, it has to be asked, is your array sorted?
Author Commented:
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;
}
Commented:
Hi,

I found the problem - really quite simple actually.

In the seq search, you have:

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;
}
}
}

This means that if the first sequential comparison fails, you return -1 right away without checking the other 14 or so elements!  Something like this works a lot better:

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

You only return -1 when everything is said and done (you've gone right through the loop.  Note that you still need the "n <= high2" not "n < high2" - this bug would be harder to catch since it would only cause 10% or so of searches to fail.

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
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!
Commented:
Author Commented:
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
Commented:
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!
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.