BinarySearch function in C++ (urjnasw xkfjjkn@CSDN)
2013-02-21 06:16
405 查看
今天学习一下基本的二叉搜索吧~~大师兄 <a href="http://infolab.tamu.edu/people/zcheng">程致远</a>说了,要是面试当场让写这个写不出来就可以直接出去了~~~
我知道的搜索算法里面最常被想到的就是BinarySearch了,最为最基本的搜索算法,写的时候还是有一些细节需要被注意到,例如计算下标时的“+1”和“-1”。下面附上面试宝典《cracking the coding interview》里面的示例写法,我觉得这是我搜到的版本里面写得最简洁明白的了。
我知道的搜索算法里面最常被想到的就是BinarySearch了,最为最基本的搜索算法,写的时候还是有一些细节需要被注意到,例如计算下标时的“+1”和“-1”。下面附上面试宝典《cracking the coding interview》里面的示例写法,我觉得这是我搜到的版本里面写得最简洁明白的了。
int BinarySearch(int[] a, int x, int low int high) { if (low > high) return -1; int mid = (low + high) / 2; if (a[mid] < x) { return BinarySearch(a, x, mid + 1, high); } else if (a[mid] > x) { return BinarySearch(a, x, low, mid - 1); } else { return mid; } }<a href = "http://blog.csdn.net/urjnasw_xkfjjkn">urjnasw xkfjjkn @ CSDN</a>
相关文章推荐
- reverse string (by urjnasw xkfjjkn @ CSDN)
- 会过去的--梁静茹 (liked by urjnasw xkfjjkn @ CSDN)
- purpose for urjnasw xkfjjkn
- binary search tree in C++
- Binary Search Tree Insertion in C++
- 【Leetcode】:108. Convert Sorted Array to Binary Search Tree 问题 in JAVA
- 【C++】【LeetCode】108. Convert Sorted Array to Binary Search Tree
- [C++]LeetCode: 122 Validate Binary Search Tree (是否是合法BST树)
- [C++]LeetCode: 53 Unique Binary Search Trees
- Exporting a function in a DLL using Microsoft Visual C++ 6.0
- Know How To Use ID_NULL Function To Search An Object In Oracle Forms
- LintCode-Search Range in Binary Search Tree
- LintCode Search Range in Binary Search Tree
- STL之二分查找 (Binary search in STL)
- How to separate the implementation and definition for template function in c++
- C++数据结构 之 二叉搜索树_Binary Search Tree
- Binary Search Tree Iterator,Reverse Nodes in k-Group
- Count occurrences of a number in a sorted array with duplicates using Binary Search
- [C++]LeetCode: 37 Construct Binary Tree from Preorder and Inorder Traversal
- Binary Order function In Unix Network Programming