Hi,
I need help for my binary search of Book list based on sorted book array result. I am still new in this field, my Bubble sort is working fine, but I got stuck at Binary Search, my program cannot find the appropriate book ID from the Sorted List for displaying output related to book ID. I really appreciate your advices.
void Swap(TYPE &first, TYPE &last)
{
TYPE temp;
temp = first;
first = last;
last = temp;
}
void BubbleSort(Book *array[], int size)
{
int smallest;
for(int first = 0; first < size - 1; first++)
{
smallest = first;
for(int current = smallest + 1; current < size; current++)
{
if(*array[current] < *array[smallest])
smallest = current;
}
Swap(array[first],array[smallest]);
}
}
bool BinarySeach(Book *array[], int size, unsigned int bookID, Book *sortedOutput)
{
int first = 0;
int last = size - 1;
int middle;
int position = -1;
while(first <= last)
{
middle = (first + last)/2;
if(array[middle] == sortedOutput)
{
position = middle;
return (true);
}
else if(array[middle] > sortedOutput)
{
last = middle - 1;
}
else
{
first = middle + 1;
}
}
return (false);
}
main program output
//sortedOutput(bookID,Bookname);
List of sorted Book
101 - Dummy Adobe Photoshop
102 - HTML,CSS
103 - Visual Studio 2008
Please enter ID to search: 102
ID not found, please try again
Thank you for your help