1、顺序查找
bool SequeueSearch(int array[], int n, int key, int &index)
{
for (int i = n-1; i >= 0; i--)
{
if (array[i] == key)
{
index = i;
break;
}
}
if (i < 0)
{
return false;
}
else
{
return true;
}
}
2、折半查找
bool BinSearch(int array[], int n, int key, int &index)
{
int low = 0;
int high = n - 1;
int mid;
while (low <= high)
{
mid = (low + high)/2;
if (array[mid] == key)
{
index = mid;
return true;
}
else if (array[mid] > key)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return false;
}
3、二叉查找树
阅读(622) | 评论(0) | 转发(1) |