I guess the two points find many people are not unfamiliar, perhaps very familiar, but in the real life and a lot of people can not correctly write its corresponding code, because the boundary conditions of the second search is difficult to control, the following us to carefully analyze the two-point search, this is only a personal opinion, if there are objections, welcome to propose. 1, two-point search can solve the problem: binary lookup can solve the unsorted array of lookup problems. As long as the array contains T (that is, the value to look for), you can eventually find it by shrinking the range that contains T. Initially, the range covers the entire array, comparing the intermediate items of the array to T, which excludes the general element and narrows it down by half. In this way, repeated narrowing of the range, the end will be found in the array t, or to determine that the scope of T is actually empty. For tables that contain n elements, the entire lookup process is approximately log (2) n times compared. Note: The binary lookup is for an ordered array. 2, two-point search related code as follows: #include <iostream> using namespace std; int binary_search (int arr[], int n, int key) {int left = 0; The first position of the array, that is, arr[0] int right = n-1; The last position of the array, that is, arr[n-1], and the size of the array is n
//Cycle conditions must pay attention to while (left <= right) { int mid = left + ((right-left) >> 1); The mid calculation here must be placed inside the while loop, or mid will not update correctly, and a shift instead of dividing by 2 can improve efficiency and prevent overflow. if (Arr[mid] > key)//array in the middle of the position is greater than the number to find, then we are in the middle of the left range of the number of { right = mid-1; } else if (Arr[mid] < key)//The position in the middle of the array is less than the number you are looking for, then we'll look for in the right range of the middle number. { left = mid + 1; } Else { return mid; The middle number is just the number you want to find. } }
//Execution flow If you go here, you don't find the number you're looking for. return-1; The test is shown as follows: void Test () { int arr[5] = {1, 3, 5, 9,}; &n bsp; int ret1 = 0,ret2 = 0,ret3 = 0; ret1 = Binary_search (arr, 5, 9); cout << ret1 << Endl; ret2 = Binary_search (arr, 5, 5); cout << Ret2 << Endl; ret3 = Binary_search (arr, 5, 2); cout << ret3 << Endl; 3, two points to find the code need to note: right=n-1-------->while (left <= right)-------->right=mid-1; Right=n-------->while (left < right)-------->right=mid; In addition: most people like to judge Arr[mid at the very beginning, but this is unwise, because the equality in most cases is a small number, written at the beginning, each cycle needs to be judged equal, waste time, inefficient.
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.