On the binary search method
In the process of learning the algorithm, in addition to understand the basic principle of an algorithm, the implementation of the way, the more important part is the complexity of the analysis algorithm. In the time complexity and space complexity, we will pay more attention to the complexity of time, often with the sacrifice of space-time method to improve time efficiency.
The complexity of time is almost concentrated in the following advantages and disadvantages:
O (1), O (log n), O (n), O (n log n), O (n2), O (nk), O (2N)
The binary lookup method is mainly to solve the problem of "finding the specified number in a pile number", and to apply the binary search method, this "pile number" must have a characteristic:
stored in the array
Orderly arrangement
So if you are using a linked list, you cannot apply a binary lookup method on it.
It does not matter whether the same elements exist in the array if the order is ascending or descending . In general, however, we hope and assume that the arrays are ascending and that the elements in the array are different .
Binary Search Program implementation:
#include <iostream>
using namespace Std;
While loop implementation
int binary_search1 (int array[], int n, int value)
{
int left = 0;
int right = n-1;
while (left <= right)//Note whether this is "<=" or "=", if "=", then the loop is changed to "yes" = middle
{
int middle = left + ((right-left) >> 2);//Direct average may overflow, so use this algorithm
if (Array[middle] > value)
{
right = middle-1;
}
else if (Array[middle] < value)
{
left = middle + 1;
}
Else
{
return middle;
}
}
return-1;
}
Recursive implementation
int binary_search2 (int array[], int left,int right, int value)
{
if (Left > right)//binary search to order
{
return-1;
}
int middle = left + ((right-left) >> 2);//Direct average may overflow, so use this algorithm
if (Array[middle] > value)
{
return Binary_search2 (array, left, middle-1, value);
}
else if (Array[middle] < value)
{
return Binary_search2 (array, middle + 1, right, value);
}
Else
{
return middle;
}
}
int main ()
{
int array[10] = {1,2,3,5,7,8,9,11,13,45};
int n = 0, num = 0,ret=0;
n = sizeof (array);
/*int left = 0, right = n-1;*/
CIN >> Num;
ret = binary_search1 (array, n, num);
/*ret = BINARY_SEARCH2 (array, left,right, num); */
if (ret = =-1)
{
cout << "Find failed! "<< Endl;
}
Else
{
cout << num << "is the first" << ret + 1 << "number" << Endl;
}
System ("pause");
return 0;
}
Run result 1:
8
8 is the 6th number.
Please press any key to continue ...
Run result 2:
17
Find failed!
Please press any key to continue ...
The defect of binary search method
The binary lookup method of O (log n) makes it a very efficient algorithm. But its flaws are so obvious. On top of its limits:
must shall be orderly , it is difficult to guarantee that our arrays are orderly. Of course, you can sort the array when you build it, but it falls to the second bottleneck: it must be an array. array read efficiency is O (1), but its efficiency in inserting and deleting an element is O (n). This results in the construction of an ordered array into inefficient things.
The better way to solve these defects is to use a two-fork search tree , preferably a self-balancing binary search tree , and Efficient (O (n logn)) to construct an ordered set of elements, as fast as a binary lookup (O (log n) ) of the search target number.
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1775691
Recursive and cyclic implementation of binary search algorithm and its defects