【发布时间】:2017-03-01 21:32:18
【问题描述】:
所以我一直在努力解决这个问题,现在需要一些关于布尔函数的帮助。我被困在 pset3 中助手的搜索部分。
我知道我的选择排序功能有效,因为我使用 printf 来检查正在排序的数字,并且我通过简单的线性搜索测试了 find 以确认它工作正常。
我的搜索功能代码如下:
bool search(int value, int values[], int n)
{
// Set upper and lower limits for mid point calculation
int max = n - 1;
int min = 0;
while (min <= max)
{
// Set the mid point of values as half the difference of the upper and lower limit.
int mid = (max - min)/ 2;
// If the array position we look at for this itteration of mid is equal to the value, return true
if (value == values[mid])
return true;
// If the mid value is less than our value, look at the right half (+1 as we dont need to look at the mid point again)
else if (value > values[mid])
return min = mid + 1;
// Same principle but for the left half of the array
else if (value < values [mid])
return max = mid - 1;
}
return false;
}
据我所知,我的逻辑对于实际计算是合理的。我尝试了许多不同的返回 false 的方法,例如 "if (value values[mid -1]" 以返回 false 但无济于事,所以我从代码在这里。任何帮助将不胜感激。
干杯
汤姆
【问题讨论】: