当前,我有一个实现插入排序的程序,然后使用二进制搜索对其进行搜索(一个int数组)。看来我目前有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吗?
答案 0 :(得分: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)
答案 1 :(得分:0)
虽然您发现检查UIView
导致无法找到索引(ind > 0)
的元素,而0
提供了解决方案,但让我们看一个简短的测试案例来验证{{ 1}}查找排序数组中的所有元素,而用户不必不断输入信息或将信息重定向到您的程序。
每当您测试算法时,都会提供一个简单的验证框架,该框架无需用户输入即可测试所有元素,数字等。以您的“ 9”数字为例。简单地包括一个初始化的数组并排序然后遍历所有值将验证(ind >= 0)
是否按预期执行。 (这也将使您免于烦恼)。在整个数组上执行搜索的简短代码可以很简单:
binarySearch()
使用/输出示例
binarySearch()
每个元素现在都列为int main (void) {
int nums[] = { 7, 4, 10, 49, 6, 12, 32, 17, 21 },
n = sizeof nums / sizeof *nums;
insertionSort (nums, n);
for (int i = 0; i < n; i++)
printf (" %2d (%s)\n", nums[i],
binarySearch (nums, n, nums[i]) >= 0 ? "found" : "not found");
}
或$ ./bin/bsearchtest
4 (found)
6 (found)
7 (found)
10 (found)
12 (found)
17 (found)
21 (found)
32 (found)
49 (found)
,并且代码自行退出。