二分查找
适用于有序表查找, 包括二叉排序树
int BinarySearch(int *array, int aSize, int key)
{
if ( array == NULL || aSize == 0 )
return -1;
int low = 0;
int high = aSize - 1;
int mid = 0;
while ( low <= high )
{
mid = (low + high )/2;
if ( array[mid] < key)
low = mid + 1;
else if ( array[mid] > key )
high = mid - 1;
else
return mid;
}
return -1;
}
散列查找
请看本博客数据结构与算法hash表文章
http://www.jianshu.com/writer#/notebooks/15612823/notes/16627768
网友评论