I have a Program that I'm using Binary Search.
Which is Working with Merge_Sort.
But what i can't figure out, is how to Try Catch the Binary Search
To see if the User. Sorted the array before using Binary Search.
If they Didn't sort the Array then they should not be allowed to use Binary Search
Code:
//function binary search using the key value to serach
int binarySearch(student sorted[], int first, int upto, int key) {
while (first < upto) {
int mid = (first + upto) / 2; // Compute mid point.
if (key<sorted[mid].student_number)
{
upto = mid; // repeat search in bottom half.
} else if (key>sorted[mid].student_number)
{
first = mid + 1; // Repeat search in top half.
} else
{
return mid; // Found it. return position
}
}
return -(first + 1); // Failed to find key
}
Code:
{
merge_sort(0,N_STUDENT-1);
cout<<" \n\t Enter the student number to search :";
cin>>key;
k=binarySearch(record, 0, N_STUDENT-1, key);
if(k>=0)
cout<<"Student Details with student Number "<<key<<"exists at the position "<<k;
else
cout<<"Not found ";
}