C中的二进制搜索不良

问题描述 投票:0回答:2

目前,我有一个程序实现插入排序,然后使用二进制搜索来搜索它(一个int数组)。我目前看起来有一个错误。

我的插入排序应按降序排序。现在,似乎缺少存储在最后位置的值。

#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?

c binary-search insertion-sort
2个回答
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;
}

尝试这一个只有一个错误,即解决 - 如果(ind> = 0)


0
投票

虽然您已经发现检查(ind > 0)导致无法在索引0找到元素并且(ind >= 0)提供了解决方案,但让我们看一个简短的测试用例,验证binarySearch()查找已排序数组的所有元素,而无需用户不断输入或重定向您的计划的信息。

每当您测试算法时,都会提供一个简单的验证框架,在没有用户输入要求的情况下测试所有元素,数字等。以你的'9'数字为例。简单地包括一个初始化的数组和排序,然后循环遍历所有值将验证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)

现在每个元素都被列为"found""not found",代码自行退出。

© www.soinside.com 2019 - 2024. All rights reserved.