C/MFC half-lookup (binary lookup)

Source: Internet
Author: User
The half-lookup method is also called the binary lookup method. it fully utilizes the order relationship between elements and adopts the Grouping Policy to complete the search task with O (logn) in the worst case. Binary search is widely used and its ideas are easy to understand. The first binary search algorithm appeared as early as 1946, but the first fully correct binary search algorithm did not appear until 1962. Bentley's book WritingCorrectPr

The premise of half-lookup is that the data has been sorted and then half-searched. For example, the sorted data is 1 5 12 35 64 78 89 123 456. You need to search for 12. First, use 12 to compare (64) and 12 <64 in the middle of the 9 numbers in the preceding order. Therefore, the data you are looking for is in the first half, that is, 1 5 12 35 64, and then compare the number in the middle of the first half of 12 (12), so we can find it twice.

The half-lookup method is also called the binary lookup method. it fully utilizes the order relationship between elements and adopts the Grouping Policy. in the worst case, it can use O (log n) to complete the search task. The goal of semi-query is to improve the search efficiency.

C functions are implemented as follows:

Int binsearch (int x, int v [], int n) {int low, high, mid; low = 0; high = n-1; while (low <= high) {mid = (low + high)/2; if (x <v [mid]) high = mid-1; else if (x> v [mid]) low = mid + 1; elsereturn mid; // Check the number of times the loop is executed // printf ("mid = % d, low = % d, high = % d n ", mid, low, high);} return-1 ;}

MFC running result:

The MFC program is as follows:

Char tmp [10] = ""; int rand_num [10]; CString str [10]; CString result; CString sort_result; void CNM_MFCDlg: OnBnClickedOk () {CEdit * pBoxOne; pBoxOne = (CEdit *) GetDlgItem (IDC_EDIT1); srand (unsigned) time (NULL); for (int x = 0; x <10; x ++) {rand_num [x] = rand () 0; str [x] = itoa (rand_num [x], tmp, 10 ); result = result + str [x] + _ T ("");} pBoxOne-> SetWindowText (result); // MessageBox (str, _ T ("program running result "), MB_ OK); result. re LeaseBuffer ();} void CNM_MFCDlg: OnBnClickedButton1 () {CEdit * pBoxTwo; pBoxTwo = (CEdit *) GetDlgItem (IDC_EDIT2); selection_sort (rand_num, 10 ); for (int x = 0; x <10; x ++) {str [x] = itoa (rand_num [x], tmp, 10 ); sort_result = sort_result + str [x] + _ T ("");} sort_result = sort_result + _ T ("~ Rn "); // UpdateData (false); pBoxTwo-> SetWindowText (sort_result); sort_result.ReleaseBuffer ();} void CNM_MFCDlg: OnBnClickedButton2 () {CEdit * pBoxThree; pBoxThree = (CEdit *) GetDlgItem (IDC_EDIT3); CString searchNum; CString showStr; char tmp [10] = ""; // selection_sort (rand_num, 10 ); pBoxThree-> GetWindowText (searchNum); int srh_n = _ ttoi (searchNum); int result = binsearch (srh_n, rand_num, 10); showStr = itoa (result, tmp, 10 ); messageBox (searchNum + _ T ("subscript of the array") + showStr, _ T ("program running result"), MB_ OK);} void CNM_MFCDlg: OnBnClickedCancel () {CDialogEx: OnCancel ();} void selection_sort (int * a, int n) {int I, j, s; for (I = 0; I <n-1; I ++) {s = I; for (j = I + 1; j <n; j ++) {if (a [j] <a [I]) {s = j ;}} swap (& a [I], & a [s]) ;}} void swap (int * p1, int * p2) {int temp; temp = * p1; * p1 = * p2; * p2 = temp;} int binsearch (int x, int v [], int n) {int low, high, mid; low = 0; high = n-1; while (low <= high) {mid = (low + high)/2; if (x <v [mid]) high = mid-1; else if (x> v [mid]) low = mid + 1; elsereturn mid; // check how many times the loop has been executed // printf ("mid = % d, low = % d, high = % d n", mid, low, high );} return-1 ;}

Remember to declare the function in the header file.

Divide n elements into two halves with roughly the same number. take a [n/2] For comparison with the x to be searched. if x = a [n/2], find x, algorithm termination. If x <a [n/2], we only need to search for x in the left half of array a (Here we assume that the array elements are in ascending order ). If x> a [n/2], we only need to search for x in the right half of array.

Binary search is widely used and its ideas are easy to understand. The first binary search algorithm appeared as early as 1946, but the first fully correct binary search algorithm did not appear until 1962. In his book Writing Correct Programs, Bentley wrote that 90% of computer experts cannot write completely Correct binary search algorithms within two hours. The key to the problem is to accurately define the boundary of each search range and determine the termination conditions, and correctly summarize the various situations of parity, in fact, we can find that the specific algorithm is intuitive.

At the beginning, the order of the array is arranged: from small to large, or from large to small. Then, we compromise the array by comparing the number in the middle with the number to be searched. For example, if I grow from small to large, I compare the number in the middle with the number to be searched. if it is small, then the number to be searched is definitely in the range from the center to the left. if it is large, the number to be searched is definitely in the range from the center to the right. Then, in the same way, the range is gradually reduced, knows until the query is found.

Address of this article: http://www.nowamagic.net/librarys/veda/detail/551,welcome.

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.