Binary search algorithm, half search algorithm!

Time:2021-7-4

This example uses binary search method to find elements of specific keywords. The user is required to input the length of the array, that is, the data length of the ordered table, and input the array elements and search keywords.

Program output to find the success or failure, as well as the success of the keyword in the array position. For example, in the ordered tables 11, 13, 18, 28, 39, 56, 69, 89, 98, 122, you can find the element with the keyword 89.

 

Implementation process:

(1)   User defined function binary_ Search (), to realize binary search.

(2)   The main() function is used as the entry function of the program.

 

The program code is as follows:

#include 

int binary_ Search (int key, int a [] int n) // user defined function binary_ search()

{

    int low,high,mid,count=0,count1=0;

    low=0;

    high=n-1;

    When (lowa [mid]) // key is greater than the middle value

            low=mid+1;    // Determine the right sub table range

        Else if (key = = a [mid]) // when the key is equal to the middle value, the search is successful

        {

            Printf ("search success\ N search% d times! a[%d]=%d",count,mid,key);    // Output the number of searches and the position of the searched element in the array

            count1++;    // COUNT1 records the number of successful searches

            break;

        }

    }

    If (COUNT1 = = 0) // judge whether the search failed

        Printf ("search failed!")// Search failed, output no found

    return 0;

}

int main()

{

    int i,key,a[100],n;

    Printf ("please enter the length of the array):";

    scanf("%d",&n);    // Enter the number of array elements

    Printf ("please input the array elements):";

    for(i=0;i

 

Results of operation:

————————

Please enter the length of the array:

10

Please enter an array element:

11 13 18 28 39 56 69 89 98 122

Please enter the element you want to find:

89

Search success!

Search twice! a[7]=89

————————

 

Technical points:

The basic idea of binary search is to search in half

First, select the record in the middle of the table, and compare its key with the given key. If it is equal, the search is successful;

If the key value is larger than the key value, the element to be found must be in the right sub table, and then continue to search the right sub table in half;

If the key value is smaller than the key value, the element to be found must be in the left sub table. Continue to search the left sub table in half.

This recursion is performed until the search is successful or failed (or the search range is 0).

 

Finally, whether you are a new professional or a beginner, you can also be advanced if you want to learn programming~

[worthy of attention] myC / C + + programming learning exchange Club[Click to enter]

Q & A, learning and communication, technical discussion, and a large number of programming resources, zero basic video is also excellent~