I'm trying to run a binary sort that sorts through a list of randomly generated numbers, but for some reason isn't working.

It will usually tell me if the highest or lowest numbers are in the array, but acts like a spaz on the middle ones. I keep fiddling with it and I get crash, or just wrong info Can someone take a look and point me in the right direction?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

// ------ PROTOTYPES -----------------------------------
void print(int []);
void sort(int []);
void populate(int []);
bool isInArray(int [], int);

// ------ CONSTS ---------------------------------------

const int ArraySize = 100;
// ****** MAIN *****************************************
int main()
{
    int array[ArraySize];
    unsigned seed = time(0);
    srand(seed);


    // Assign random numbers between 1 and 100 to each element of the array
    populate(array);

    print(array);
    cout << endl;

    sort(array);

    print(array);
    cout << endl;

    // check the array for values 0, 25, 50, 75, 100
    for (int i = 0; i <= 100; i += 25)
    {

        cout << i << " is ";

        if (!isInArray(array, i))
        {
             cout << "not ";
        }
        cout << "in the array" << endl;
    }
}

// ------ FUNCTION Binary Search -----------------------
//
// -----------------------------------------------------
bool isInArray(int array[], int num)
{

    int lower = 0;
    int higher = ArraySize - 1;
    int middle;


    while(lower <= higher)
    {
        middle = (lower + higher)/2;

        if(array[lower] == num || array[ArraySize - 1] == num)
        {
            return true;
        }
        if(num < array[middle])
        {
            higher = middle - 1;
        }
         else if(num > array[middle])
        {
            lower = middle + 1;
        }
return false;
    }

}

// ------ FUNCTION POPULATE ----------------------------
// Generate 100 random numbers between 0 and 100
// -----------------------------------------------------
void populate(int array[])
{
    for(int i = 0; i < ArraySize; i++)
    {
        array[i] = rand() % 100;
    }

    return;
}

// ------ FUNCTION SORT --------------------------------
// Sort numbers from lowest to highest
// -----------------------------------------------------
void sort(int array[])
{

    int temp = array[0];
    bool swapped;

    do
    {
        swapped = false;

        for(int i = 0; i < ArraySize -1; i++)
        {
            if(array[i] < array[i + 1])
            {
                temp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = temp;
                swapped = true;
            }
        }

    }while(swapped);

    return;
}

// ------ FUNCTION PRINT -------------------------------
//
// -----------------------------------------------------
void print(int array[])
{
    int counter = 0;

    for(int i = 0; i < ArraySize; i++)
    {
        cout << array[i] << " ";
        counter ++;

        if(counter > 1 && counter % 25 == 0)
        {
            cout << endl;
            counter = 0;
        }

    }

    return;
}

OUTPUT:
69 84 13 30 57 98 2 56 97 28 31 50 21 84 45 93 55 60 2 47 14 60 69 46 45
60 90 3 94 89 20 55 65 67 33 8 13 21 78 39 40 24 71 45 18 25 7 84 32 38
64 27 82 75 82 84 63 81 81 41 7 34 28 25 18 14 79 37 75 10 13 19 59 47 78
1 39 93 7 72 91 49 72 6 44 71 64 27 92 32 19 77 33 23 84 6 8 31 22 85

98 97 94 93 93 92 91 90 89 85 84 84 84 84 84 82 82 81 81 79 78 78 77 75 75
72 72 71 71 69 69 67 65 64 64 63 60 60 60 59 57 56 55 55 50 49 47 47 46 45
45 45 44 41 40 39 39 38 37 34 33 33 32 32 31 31 30 28 28 27 27 25 25 24 23
22 21 21 20 19 19 18 18 14 14 13 13 13 10 8 8 7 7 7 6 6 3 2 2 1

0 is not in the array
25 is not in the array
50 is not in the array
75 is not in the array
100 is not in the array

Process returned 0 (0x0) execution time : 0.546 s
Press any key to continue.

From the prints, it seems that your sorting function sorts the number in decreasing order, but your binary search function assumes that the array is sorted in increasing order (from lowest to highest). You need to reverse the logic in one of these two methods if you want things to work.

Hmmm, well I reversed the order on my sort but sadly, it's still acting wonkalicous...

69 65 9 30 98 23 85 70 15 36 85 63 45 28 63 83 0 3 83 72 91 47 33 84 77
72 48 7 85 15 28 37 83 45 90 36 7 46 91 36 10 69 46 21 2 50 54 25 43 22
23 3 87 57 96 55 58 89 77 74 38 33 5 29 56 97 87 89 64 97 61 53 14 25 21
56 13 55 91 1 8 93 6 76 75 8 78 25 95 61 87 63 45 51 12 50 96 49 46 5

0 1 2 3 3 5 5 6 7 7 8 8 9 10 12 13 14 15 15 21 21 22 23 23 25
25 25 28 28 29 30 33 33 36 36 36 37 38 43 45 45 45 46 46 46 47 48 49 50 50
51 53 54 55 55 56 56 57 58 61 61 63 63 63 64 65 69 69 70 72 72 74 75 76 77
77 78 83 83 83 84 85 85 85 87 87 87 89 89 90 91 91 91 93 95 96 96 97 97 98

0 is not in the array
25 is not in the array
50 is not in the array
75 is not in the array
100 is not in the array

Process returned 0 (0x0) execution time : 0.094 s
Press any key to continue.

My apologies, I made a bone headed mistake just prior to reversing the order. Fixed it, reversed the order per your suggestion and it works like gang busters!!!! Thanks so much. Very helpful in understanding the way the sort works!

You marked this solved, but you're not really done.

Line 64 in your search routine is not right.
if(array[lower] == num || array[ArraySize - 1] == num)
This tests the lower end index and the last value in the array (so what good is higher doing?) yet you then test array[middle] to determine which bounding index to move.
The correct comparison is:
if(array[middle] == num )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.