BinarySearch problems
bool binarySearch(int array[], int Size, int value, int& position)
{
while (position <= Size) {
int mid = (position + Size) / 2; // compute mid point.
if (value > array[mid])
position = mid + 1; // repeat search in top half.
else if (value < array[mid])
Size = mid - 1; // repeat search in bottom half.
else
{
position = mid;
return true; // found it. return position /////
}
}
position = (position - 1);
return false; // failed to find value
}
To test this, I've got a function that generates a list of random numbers (using another pre-defined function random) to generate a unique array of numbers between a certain range.
bool randArray(int array[], int listSize, int lowerList, int upperList)
{
for(int i = 0; i < listSize; i++) //Initalze all member items to 0 (used for passing the 'sorted' list
array[i] = 0;
int RandNum;
int ToPut = 0;
for(int i = 0; i < listSize; i++)
{
do
{
RandNum = random(lowerList, upperList);
} while(binarySearch(array, i, RandNum, ToPut) == true);
for(int j = i + 1; j > ToPut; j--)
swap(array[j], array[j-1]);
array[ToPut] = RandNum;
}
}
This one has really got me stumped. Thank you all so much in advance for your time.
P.s
My first ever post! Yay! I'm a part of a new community :) hehe

