gpt4 book ai didi

c - C 中的二分查找有缺陷

转载 作者:行者123 更新时间:2023-11-30 14:39:53 24 4
gpt4 key购买 nike

目前,我有一个程序实现插入排序,然后使用二分搜索来搜索它(一个整数数组)。我目前似乎有 1 次错误。

我的插入排序应按降序排序。现在,似乎最后一个位置存储的值丢失了。

#include <stdio.h>
void insertionSort(int nums[], int size)
{
int i, key, j;
for (i = 1; i < size; i++)
{
key = nums[i];
j = i - 1;

/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && nums[j] > key)
{
nums[j + 1] = nums[j];
j = j - 1;
}
nums[j + 1] = key;
}
}

int binarySearch(int nums[], int size, int searchVal)
{
int l = 0, r = size - 1;

while (l <= r)
{
int m = l + (r - l) / 2;

// Check if x is present at mid
if (nums[m] == searchVal)
return m;

// If x greater, ignore left half
if (nums[m] < searchVal)
l = m + 1;

// If x is smaller, ignore right half
else
r = m - 1;
}

// if we reach here, then element was
// not present
return -1;
}

int main()
{
int n;
printf("Enter the number of elements (between 1 and 50) in the array: \n");
scanf("%d", &n);
int i, nums[n];
printf("Enter %d positive integers: \n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &nums[i]);
}
int x = 0;
insertionSort(nums, n);
printf("Enter a positive integer or -1 to quit: \n");
scanf("%d", &x);
do
{
int ind = binarySearch(nums, n, x);
if (ind > 0)
{
printf("Found\n");
}
else
{
printf("Not Found\n");

}
printf("Enter a positive integer or -1 to quit: \n");
scanf("%d", &x);
} while (x != -1);

return 0;
}

结果:

Enter the number of elements (between 1 and 50) in the array:
9
Enter 9 positive integers:
7
4
10
49
6
12
32
17
Enter a positive integer or -1 to quit:
4
Not Found
Enter an positive integer -1 or to quit
12
Found
Enter a positive integer or -1 to quit:
5
Not Found
Enter a positive integer or -1 to quit:
49
Found
Enter a positive integer or -1 to quit:
-1

如您所见,一切正常,但第一个测试是我测试数字 4。有人知道为什么我落后 1 吗?

最佳答案

#include <stdio.h>

void insertionSort (int nums[], int size)
{
int i, key, j;
for (i = 1; i < size; i++) {
key = nums[i];
j = i - 1;

/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && nums[j] > key) {
nums[j + 1] = nums[j];
j = j - 1;
}
nums[j + 1] = key;
}
}

int binarySearch (int nums[], int size, int searchVal)
{
int l = 0, r = size - 1;

while (l <= r) {
int m = l + (r - l) / 2;

// Check if x is present at mid
if (nums[m] == searchVal)
return m;

// If x greater, ignore left half
if (nums[m] < searchVal)
l = m + 1;

// If x is smaller, ignore right half
else
r = m - 1;
}

// if we reach here, then element was
// not present
return -1;
}

int main ()
{
int n;
printf
("Enter the number of elements (between 1 and 50) in the array: \n");
scanf ("%d", &n);
int i, nums[n];
printf ("Enter %d positive integers: \n", n);
for (i = 0; i < n; i++) {
scanf ("%d", &nums[i]);
}
int x = 0;
insertionSort (nums, n);
printf ("Enter a positive integer or -1 to quit: \n");
scanf ("%d", &x);
do {
int ind = binarySearch (nums, n, x);
if (ind >= 0) {
printf ("Found\n");
} else {
printf ("Not Found\n");

}
printf ("Enter a positive integer or -1 to quit: \n");
scanf ("%d", &x);
} while (x != -1);

return 0;
}

试试这个只需解决一个错误 - if(ind>=0)

关于c - C 中的二分查找有缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55861055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com