This article describes the C language to achieve the two descending sequence to find a certain number of methods to share for everyone to use for reference. The specific methods are as follows:
In general, this problem is a very difficult question in the binary search method.
The topics are as follows:
An array is left by a descending sequence of several bits to form, such as {4, 3, 2, 1, 6, 5} is moved 6 by {5, 4, 3, 2, 1, two} to find a number in this array.
The implementation code is as follows:
int array[] = {4, 3, 2, 1, 6, 5};
const int size = sizeof array/sizeof *array;
int findminnumber (int (&array) [size], int start, int last, int dest)
{
int mid = (Last-start)/2 + start;
int result;
if (Start > last) {
return-1
}
if (array[mid] = = dest) {result
= Mid;
return result;
}
if (Array[mid] <= Array[start]) {
if (dest > Array[mid] && dest <=]) {Last
= Array[start ;
result = Findminnumber (array, start, last, dest);
else {
start = mid + 1;
result = Findminnumber (array, start, last, dest);
}
else if (Array[mid] > Array[start]) {
if (Dest < Array[mid] && dest >= array[last]) {
start = mid + 1;
result = Findminnumber (array, start, last, dest);
else {Last
= mid-1;
result = Findminnumber (array, start, last, dest);
}
return result;
}
The results of the program run as shown in the following illustration:
I hope this article is helpful to the learning of C program algorithm design.