binary search

Binary search, also known as binary search, is a highly efficient search method. However, the halved search requires that the linear table must adopt a sequential storage structure, and the elements in the table are arranged in order by key.

Find process

edit
First, assuming that the elements in the table are arranged in ascending order, compare the keyword recorded in the middle of the table with the search keyword, if the two are equal, the search is successful; otherwise, the table is divided into two sub-tables before and after the middle position record is used, if If the key recorded in the middle position is greater than the search key, the previous sub-table is further searched, otherwise the latter sub-table is further searched. The above process is repeated until a record that satisfies the condition is found, and the search succeeds, or until the subtable does not exist, and the search is unsuccessful at this time. 4

Algorithm Requirements

edit
1. A sequential storage structure must be used.
2. Must be sorted by keyword size.

Description of the steps of the bisection algorithm

① First determine the middle position of the entire search interval mid = ( left + right ) / 2

② Compare the keyword value to be checked with the keyword value in the middle position;

If they are equal, the search succeeds

If it is greater than, continue to search in half in the last (right) half area

If it is less than, continue to search in half in the first (left) half area

③ Repeat the above steps according to the halving formula for the determined reduced area.

Finally, the result is obtained: either the search succeeds or the search fails. The storage structure of the halved search is stored in a one-dimensional array. Example of halving search algorithm

For a given sequence (ordered) { 3, 5, 11, 17, 21, 23, 28, 30, 32, 50, 64, 78, 81, 95, 101}, according to the halved search algorithm, find the key value of 81 data element.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324962920&siteId=291194637