Implement binary search and implement binary search using standard library functions
Algorithm ideas: 1. Sort arrays (from small to large); 2. Compare each time with the number mid in the middle. If they are equal, they can be directly returned,
If it is larger than the mid value, search for the larger side; otherwise, search for the smaller side.
Input: sorted array-arrayname [], array size-array_size, searched value-searchnumber
Return: locate and return its position in the array; otherwise,-1 is returned.
Binary Search code implementation
Int binary_search (int arrayname [], intarray_size, int searchnumber)
{
Intlow = 0, high = array_size-1, mid;
While (low <= high)
{
Mid = (low + high)/2; // obtain the intermediate position
If (arrayname [mid] = searchnumber)
Returnmid; // returns the corresponding location
If (arrayname [mid]> searchnumber)
High = mid-1; // if it is greater than the key, search for the lower part.
Else
Low = mid + 1; // if it is smaller than the key, search for it in a higher position.
}
Return-1;
}
Sample Code:
(1) Sort unordered Arrays
(2) binary search for the number to be searched
(3) Display Search Results
# Include <iostream>
# Include <algorithm>
Using namespace std;
Int binary_search1 (int arrayname [], intarray_size, int searchnumber)
{
Intlow = 0, high = array_size-1, mid;
While (low <= high)
{
Mid = (low + high)/2; // obtain the intermediate position
If (arrayname [mid] = searchnumber)
Returnmid; // returns the corresponding location
If (arrayname [mid]> searchnumber)
High = mid-1; // if it is larger than searchnumber, search for the lower part.
Else
Low = mid + 1; // if it is smaller than searchnumber, search for it in a higher position.
}
Return-1;
}
Int main ()
{
Int a [10] = {9, 6,}, I = 0;
Printf ("every element in the array before being sorted! \ N ");
For (I = 0; I <10; I ++)
Cout <a [I] <"\ t ";
Cout <endl;
Sort (a, a + 10 );
Printf ("every element in the sorted array! \ N ");
For (I = 0; I <10; I ++)
Cout <a [I] <"\ t ";
// Search for numbers in the array
I = binary_search1 (a, 10, 5 );
If (I)
{
Cout <"the position of the number to be searched in the array is" <I <endl;
Cout <"the number is" <a [I] <endl;
}
Else
Cout <"No such number! "<Endl;
// Search for numbers that do not exist in the array
I = binary_search1 (a, 10, 20 );
If (I> 0)
Cout <a [I] <endl;
Else
Cout <"No such number! "<Endl;
System ("pause ");
Return 0;
}
Use the binary search function in the standard library to implement binary search
# Include <iostream>
# Include <algorithm>
Using namespace std;
Int main ()
{
Int a [10] = {9, 6,}, I = 0;
Printf ("every element in the array before being sorted! \ N ");
For (I = 0; I <10; I ++)
Cout <a [I] <"\ t ";
Cout <endl;
Sort (a, a + 10 );
Printf ("every element in the sorted array! \ N ");
For (I = 0; I <10; I ++)
Cout <a [I] <"\ t ";
// Search for numbers in the array
If (binary_search (a, a + 10, 5 ))
{
Cout <"the number to be searched is in the array" <endl;
}
Else
Cout <"No such number! "<Endl;
// Search for numbers that do not exist in the array
If (binary_search (a, a + 10, 20 ))
Cout <a [I] <endl;
Else
Cout <"No such number! "<Endl;
System ("pause ");
Return 0;
}