Binary Search

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.
        } 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
}



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 ";
        }

Are you asking how to know is an array is sorted?
1
2
for(int i = 0; i < array_length-1; i++) if(array[i] > array[i+1]) return false;//> for ascending
return true;
Yeah to see if the Array is Sorted. and if its not give out an Error.. if it is return the Search
Well then if you name the function I wrote for you bool is_sorted(student array[]), the code is
1
2
3
4
while(!is_sorted(record))
   choose_a_method_and_sort(record);

do_binary_search(record)
Last edited on
2 3 5 10 19 21
Are these numbers sorted? How do you know?

3 2 6 11 20 19
Are these numbers sorted? When did you know?

Your brain is the smartest computer; remember, your just teaching the computer how to do something you already know how to do...
Last edited on
with bool is_sorted(student array[])
And
1
2
3
4
while(!is_sorted(record))
   choose_a_method_and_sort(record);

do_binary_search(record)

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
else if ( 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 ";
		}
Last edited on
You only need to sort the array once.
1
2
3
4
5
6
//pseudo code
if( !is_sorted(record) )
  choose_a_method_and_sort(record);
loop {
  search_for_values(record);
}

Does this do what you want?
I have menus in the program so each function is in a different menu
Topic archived. No new replies allowed.