C ++ binary search-from programming Pearl

Source: Internet
Author: User

Binary search algorithm (Binary search) is a common algorithm. From the perspective of <programming Pearl>, we can see new gains.
Let's look at the code. The following is a common implementation code:
Int binary_search (int * a, int num, int t)
{
Int start = 0, end = num-1;

While (end> = start ){
Int middle = (start + end)/2;
Int tmp = a [middle];
If (tmp <t ){
Start = middle + 1;
} Else if (tmp> t ){
End = middle-1;
} Else {
Return middle;
}
}

Return-1;
}

The optimized code is ):

Int binary_search (int * a, int num, int t)
{
Int low =-1, high = num-1;

While (low + 1! = High ){
Int middle = (low + high)/2;
If (a [middle] <t ){
Low = middle;
} Else {
High = middle;
}
}

If (a [high]! = T)
Return-1;
Else
Return high;
}
 
If you look at this Code directly, you may not know what is going on. However, after using the "program verification" method mentioned in the book, the principle is obvious. The modified code is as follows:

1 int binary_search (int * a, int num, int t)
2 {
3 int low =-1, high = num-1;
4
5 // invariant: low 6 while (low + 1! = High ){
7 int middle = (low + high)/2;
8 if (a [middle] <t ){
9 low = middle;
10} else {
11 high = middle;
12}
13}
14 // assert: low + 1 = high & a [low] <t & a [high]> = t

15
16 if (a [high]! = T)
17 return-1;
18 else
19 return high;
20}
21
The idea of "program verification" can be briefly described as: whether it is to verify a function, a statement, a control structure (loop, if branch, etc ), both can use two assertions (Pre-and Post-condition) to achieve this goal. Preconditions are the conditions that should be established before the code is executed. the correctness of the post conditions must be ensured after the code is executed. (Ps: assertion is also a verification method)
The principle of the above Code is to specify a range (low, high], if the limit is a [low] <t & a [high]> = t & high = low + 1, there are two situations: 1. a [high] = t; 2. the element equal to t does not exist. Because array a must satisfy Condition a [low] <t & a [high]> = t, the algorithm must set the range (-1, num-1] to (low, low + 1].
1. Execute Code 6 ~ When the row is 17, always ensure that low 2. 6 ~ After 17 rows, make sure that the condition a [low] <t & a [high]> = t & high = low + 1, because the condition for loop exit is high = low + 1, the loop always guarantees the above 1st.
After such analysis, we can have a better grasp of the correctness of the program, and the program is also easier to understand.

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.