C language implements two descending series to find a certain number _c language

Source: Internet
Author: User
Tags int size

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.