[Learning the classic algorithm series from scratch] Rule sharding policy instance-Binary Search

Source: Internet
Author: User

1. Introduction to binary search algorithms

Binary Search is a search algorithm used to search for a specific element in an ordered array. The search process starts from the intermediate element of the array. If the intermediate element is the element to be searched, the search process ends. If a specific element is greater than or less than the intermediate element, search in the half where the array is greater than or less than the intermediate element, and compare it from the intermediate element as before. If the array in a step is empty, it indicates that no value can be found. This search algorithm reduces the search range by half for each comparison. Half-fold search reduces the search area by half each time, and the time complexity is logn ).

The advantage of binary lookup is that the number of queries is small, the search speed is fast, and the average performance is good. The disadvantage is that the table to be queried must be an ordered table, and it is difficult to insert or delete the table. Therefore, the binary search method is suitable for searching frequently ordered lists without frequent changes.

2. Binary Search Algorithm requirements

(1) The sequential storage structure must be used.

(2) They must be sorted by keyword size.

3. Binary Search Algorithm Flowchart


Figure 1 binary search algorithm Flowchart

4. C language implementation

#include <stdio.h>int binary_search_recursion(const int array[], int low, int high, int key){int mid = low + (high - low)/2;if(low > high)return -1;else{if(array[mid] == key)return mid;else if(array[mid] > key)return binary_search_recursion(array, low, mid-1, key);elsereturn binary_search_recursion(array, mid+1, high, key);}}int binary_search_loop(const int array[], int len, int key){int low = 0;int high = len - 1;int mid;while(low <= high){mid = (low+high) / 2;if(array[mid] == key)return mid;else if(array[mid] > key)high = mid - 1;elselow = mid + 1;}return -1;}int main(){const int num_array[] = {2,4,6,8,10,12,14,16,18,20};int key;int key_pos;int array_size = sizeof(num_array)/sizeof(int);for(int i=0;i<array_size;i++){printf("%d  ",num_array[i]);if(i % 5 == 0)printf("\n");}printf("\n");while(1){printf("Put in the number you want to find:\n");scanf("%d",&key);//key_pos = binary_search_recursion(num_array, 0, array_size, key);key_pos = binary_search_loop(num_array, array_size, key);printf("The position of key is %d\n", key_pos);}}

Figure 2 program results

5. Causes of boundary errors

The boundary of the binary search algorithm is generally divided into two types: Left closed and right open intervals, such as [low, high) and left closed and right closed intervals, such as [low, high]. Note that the initialization conditions of the circulating body in vitro must comply with the same interval rules as those of the circulating body. That is to say, if the cyclic body is initialized, it is based on the left closed and right open intervals, so the internal iteration of the loop body should also be like this.


Reprinted please indicate the author and Article Source: http://blog.csdn.net/jasonding1354/article/details/38176171

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.