c上的二进制搜索,while循环
在C语言中,我无法从二进制搜索代码中得到一些东西c上的二进制搜索,while循环,c,search,binary,C,Search,Binary,在C语言中,我无法从二进制搜索代码中得到一些东西 int binarySearch(int a[], int n, int x) { int low=0, mid, high=n-1; while(low <= high) { mid = (low + high) / 2; if (x < a[mid]) high = mid - 1; else if (x > a[mid])
int binarySearch(int a[], int n, int x)
{
int low=0, mid, high=n-1;
while(low <= high)
{
mid = (low + high) / 2;
if (x < a[mid])
high = mid - 1;
else if (x > a[mid])
low = mid + 1;
else
return mid;
}
return -1;
}
intbinarysearch(inta[],intn,intx)
{
int低=0,中,高=n-1;
while(低a[中])
低=中+1;
其他的
中途返回;
}
返回-1;
}
为什么while循环while(left采用一个简单的
int a[1] = {5};
printf("%d\n", binarySearch(a, 1, 5));
使用while(低<高)
,代码打印-1(未找到-回答错误)
使用while(低部分1-快速回答此特定问题
这不是对二进制搜索的完整总结,但我将简而言之以解决这个问题。
这两个while循环条件的关键区别,给定
1.相应地更新低位
(左侧
)和高位
(右侧
)指针。通过“相应地”,请参见第3部分。
2.假设低
(左
)和高
(右
)
3.假设目标存在于数组中
while(low)如果没有完整的代码,这很难说。Iv'e添加了代码。我看到了,我看到它不起作用,但我想知道为什么,谢谢。你真的在问“是@Olaf吗?好吧,我能想到两种变体的算法表现相同……所以问题是”算法是否与相同?使用一个元素数组在代码中单步执行肯定会说明为什么
public int binarySearch(int[] nums, int target){
int left = 0, right = nums.length - 1;
// please pay attention to the initial condition of right(ptr)
while(left <= right){
// to floor the mid
int mid = left + (right - left) / 2;
// to check whether the middle element is equal to the target in every iteration
if(nums[mid] == target) return mid;
else if(target > nums[mid]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
public int binarySearch(int[] nums, int target){
// please pay attention to the initial condition or the right(ptr)
int left = 0, right = nums.length;
while(left < right){
int mid = left + (right - left) / 2;
// please pay twice attention to the equality case
if(target > nums[mid]) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
public int binarySearchWithFlooringMid(int[] nums, int target){
// please pay attention to the initial condition of right(ptr)
int left = 0, right = nums.length - 1;
while(left <= right){
// to floor the mid
int mid = left + (right - left) / 2;
// to check whether the middle element is equal to the target in every iteration
if(nums[mid] == target) return mid;
else if(target > nums[mid]) {
left = mid + 1;
} else {
right = mid;
}
}
return -1;
}
public int binarySearchWithCeilingMid(int[] nums, int target){
int left = 0, right = nums.length - 1;
while(left <= right){
// to ceil the mid
int mid = left + (right - left + 1) / 2;
// to check whether the middle element is equal to the target in every iteration
if(nums[mid] == target) return mid;
else if(target > nums[mid]) {
left = mid;
} else {
right = mid - 1;
}
}
return -1;
}
public int binarySearchLeftmost(int[] nums, int target){
int left = 0, right = nums.length;
while(left < right){
int mid = left + (right - left) / 2;
// please pay twice attention to the equality case
if(target > nums[mid]) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
public int binarySearchRightmost(int[] nums, int target){
int left = 0, right = nums.length;
while(left < right){
int mid = left + (right - left) / 2;
// please pay twice attention to the equality case
if(target < nums[mid]) {
right = mid;
} else {
left = mid + 1;
}
}
return right - 1;
}