In the second chapter, several concrete examples are given and several algorithms of different complexity are given. I wrote some of the books in the example of some not given the algorithm code and compile debugging through, while for most of the after-school exercises also did the answer and compile, the following is according to the problem of code Analysis ordered array lookup elements
Problem Description a[] is an array in which the elements are sorted in order, requiring that an element x be found in A and returned with its subscript algorithm 1 order lookup
This is the most direct method, that is, traversing from 0 to N-1
int sequensearch (const int a[], int X, int N)
{for
(int i = 0; i < N; i++)
if (a[i] = x) return
I;
return NotFound;
}
Complexity Analysis :
The worst case needs to traverse from 0 to N-1 altogether N times, the complexity of the algorithm is linear, that is, O (N), but because the array a itself is an ordered array, algorithm 1 does not take full advantage of the N/2. iterative method of algorithm 2 pair-finding
Each time the element x to be searched is compared to the intermediate element A[center] in the array, if X>a[center] continues to find between A[center] and a[n-1, and if X<a[center] continues to find between A[0 and A[center] , until x equals A[center] or x is not found
Algorithm 2 iterative lookup
//input: a[]: ordered array to find, required sorted; X: Data to find; N array size
//return:x subscript (found) in array or-1 (not found)
int BinarySearch (const int a[], int X, int N)
{
int low, high, center;
Low = 0;
High = N-1;
While [Low <= high]
{
Center = (low + high)/2;
if (X < A[center]) high
= Center-1;
else if (X > A[center]) Low
= center + 1;
else return center;
}
return notfound;
}
Degree of complexity analysis
The worst case is that X does not exist, you will always find the subscript low> high
, because the lookup range is reduced to half the time in each iteration in the while loop, assuming N is 64, the lookup range for each iteration is 64 32 16 8 4 2 11, which is log64+1, and for any number n, the iteration is required [logn]+1, where [Logn] is not more than the maximum integer of Logn, so it is apparent that the algorithm 2 is of a level of complexity, that is, O (logn)
Notice here that the jump condition of the iteration (Low <= High), and the "propulsion process" for each iteration to the next iteration, can try to change low = center +1 to Low = center can cause the wireless loop to not jump out because when low=high-1, At this point, the center is equal to low, which causes the iteration to not proceed, and using low = center +1 ensures that a forced push is generated between each iteration, so that an iterative control algorithm can be used to iterate through the conditions in the while the recursive method of 3 pair-lookup is 1
The basic idea is the same as the algorithm 2, only in algorithm 2 is a while () to control the number of iterations, and here in the recursive way, through the final set of return conditions to control the number of recursion
Recursive algorithm for 3 pairs of//input:a to find
ordered array, x to find elements, low lookup range lower subscript, high lookup range upper bound subscript
//return:x in A subscript (found) or-1 (not found)
int BINARYSEARCH_REC (const int a[], int X, int low, int high)
{
if (low = = high) return
(A[low] = = x)? Low:notfound;
int center;
Center = (low + high)/2;
if (X < A[center])
{high
= center-1;
Return Binarysearch_rec (A, X,low, High);
}
else if (X > A[center])
{Low
= center + 1;
Return Binarysearch_rec (A, X, N, high);
}
else return center;
}
Degree of complexity analysis
You can set the array to N when finding X takes T (n) time, obviously t (1) = 1, while observing each iterative process, with the worst-case scenario, where x is less than a[center per iteration or greater than a[center], then at least T (N/2) time is required in both cases, Observing statements such as Center = (low + high) and while judgment consumes only O (1) time, so T (N) can consist of two parts, i.e.
T (n) =t (N/2) +1 t (n) = t (N/2) + 1
That is, we get the recursive formula for T (N), and then by T (1) = 1, we can get
T (2) =t (1) +1=1+1 T (2) = t (1) + 1 = 1 + 1
T (=t) (2) +1=2+1 T (2^2) =t (2) + 1 = 2 + 1
T (=t) +1=3+1 t (2^3) = t (2^2) + 1 = 3 + 1
... ...
from which the t (N) = Logn +1 is not strictly summed up, so the complexity is O (logn).
Or, to put it another way, it's obviously similar to algorithm 2, which reduces the range of lookup arrays by half after each recursive, so the complexity is O (Logn),
In recursion, it is particularly necessary to be careful of the final return condition and the way each recursive is pushed, similarly, the algorithm 2, if low = center + 1 is changed to Low = center. It also happens. Unable to generate recursive return conditions, infinite recursion causes stack overflow algorithm 4-pair-lookup recursive notation 2
In algorithm 3, by judging low = = high as the last recursive return condition, here you can like algorithm 2 to change the recursive return condition to judge Low <=
Recursive algorithm for 2 pairs of//input:a to find
ordered array, x to find elements, low lookup range lower subscript, high lookup range upper bound subscript
//return:x in A subscript (found) or-1 (not found)
int BINARYSEARCH_REC2 (const int a[], int X, int low, int high)
{
if (low <= high)
{
int center;
Center = (low + high)/2;
if (X < A[center])
{high
= center-1;
Return Binarysearch_rec (A, X, N, high);
}
else if (X > A[center])
{Low
= center + 1;
Return Binarysearch_rec (A, X, N, high);
}
else return center;
}
return notfound;
}
Degree of complexity analysis
is obviously the same as in Algorithm 3, for O (LOGN)