【问题标题】:Defective Binary search in CC中有缺陷的二分搜索
【发布时间】:2019-04-26 05:01:02
【问题描述】:

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

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

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

【问题讨论】:

  • how to debug small programs。这里的第一步是将排序部分和搜索部分分开,并验证问题是否在您认为的位置。
  • “输入9个正整数”,给定8个?
  • if (ind &gt; 0) 应该是 if (ind &gt;= 0)
  • return m;if (ind &gt; 0) 将永远找不到索引 0 处的元素...

标签: c binary-search insertion-sort


【解决方案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)

【讨论】:

    【解决方案2】:

    虽然您发现检查 (ind &gt; 0) 导致无法找到索引处的元素 0(ind &gt;= 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",并且代码自行退出。

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 2017-07-02
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多