您的位置:首页 > 其它

The bubble sort and Binary search

2007-02-04 17:42 567 查看
Binary search is said to be the oldest way to make a search. However, the difficulty of achieving it has been acknowledged by the whole IT fields. Knuth put the algorithm forward as early 1946 in the book "Sorting and Searching" but not until 1962 did the first program without any bugs come to the world. About only 10% programmers are able to write it without any mistake.

#include <stdio.h>
#define NUMMAX 7

void main()
int low = 0, high = NUMMAX - 1;
3 int i = 0;
4
5
21 if(num[i] != target)
22 printf("%s\n", "Not exit");
23 else
24 printf("Got it, the position is %d\n", i);
3.1 In each time of loop, low is iterated by i + 1, not i, or the search won't get the last element of your array. The same to high.
3.2 Simple as it may seem, let me analyse the whole structure in detail. It may have something to do with the knowledge of Discrete Mathmatics.
The simple block indicates a general principle for iterative process, initialization, saving and terminal.
Initialization: Iterative process begins after the invariant (or assertion) is initialized to be true, often by give a initial value to the argument or so.
Saving: In the iterative process, we must make sure the inviariant always be correct. So it can be proved by Mathematical Induction that both before and after the iteration, the assertion is always correct.
Terminal: Wherever the loop ends, the invariant is always correct.
All the 3 steps above prove the truth that the loop can be properly ended with the correction of invariant. To do this, let us take a look at the validation of any function.
To validate whether a funtion or assertion is correct or not, the most popular way is Syllogism.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: