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.
#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.
相关文章推荐
- merge sort and binary search recursive version
- What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?
- Algorithms Review: Divide and Conquer(Binary Search & Merge Sort)
- 利用二叉树的中序遍历和后序遍历序列构造一个二叉树Search results for Construct Binary Tree from Inorder and Postorder Traversa
- Binary Search Tree--find Lowest Common Ancestor--C++ and Java
- 中序遍历二叉排序树:BinarySearchTree:Create a tree and InorderTree
- AlphaGo论文的译文,用深度神经网络和树搜索征服围棋:Mastering the game of Go with deep neural networks and tree search
- 【CF】438E. The Child and Binary Tree
- discern the words in the picture and then search it on the web
- Quick sort and the Kth number
- Find the second largest element in a Binary Search Tree
- Use bubble sort to sort the input intergers
- Sqequential Search And Binary Search
- Get to the Top on Google: Tips and Techniques to Get Your Site to the Top of the Search Engine Ranki
- CodeForces 438 E.The Child and Binary Tree(生成函数+FFT)
- Binary search and its variation
- 【二分】Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string
- gxx_slide之The Child and Binary Tree
- Kmeans based indexing and Asymmetric Distance Computation for ANN search (Binary Local Feature): par
- A class that implements the Set interface using a binary search tree