I have a Binary Search function Working..
But i don't know how to set a function which will see first if the Array was either Merge Sort or Select Sort, if it is.. it will contuie on with the Binary Search If Not it will Ask you to Merge Sort or Select Sort.. and give them an a Y/N to Merge or Select Sort.. and then allow them to Search...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/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.
} elseif (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
}
This is the Code that is inside Main
1 2 3 4 5 6 7 8 9 10 11
{
//Binary Search Student Records when Select Sort is Done.
selectSort(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 ";
}
where in the code do i place them two ? do i place the while Statement inside here ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
elseif ( choice == 4)
{
while(!is_sorted(record))
choose_a_method_and_sort(record);
do_binary_search(record)
//Binary Search Student Records when Merge Sort is Done.
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 ";
}