【问题标题】:C++ Binary Search function does not printC++ 二分搜索功能不打印
【发布时间】:2017-10-27 09:38:14
【问题描述】:

谁能帮我修复这个代码? main 确实填充了一个从 1 到 1000 的 1000 个随机数的数组,当它进入函数时,它按升序排序并遵循二进制搜索,但问题是它没有打印出任何东西。

int f10(int array[], int size)
{
    int temp; // temporary swap variable 
    cout << "Ascending Order:" << endl;
    for (int x = 1; x <= size; x++)
    { //how times 

        for (int j = 1; j <= size; j++)
        {

            if (array[j] > array[j + 1])
            {

                //we need to swap
                temp = array[j]; //temp is holding the first value 
                array[j] = array[j + 1]; //
                array[j + 1] = temp; //temp holds the second value
            }
        }
    }
    for (int x = 1; x <= size; x++)
    {
        cout << array[x] << ", ";
    }

    int value;
    cout << "\nGimme a number to search:\n\t";
    cin >> value;

    int left = array[1], right = array[1000];
    while (left < right)
    {

        int middle = (left + right) / 2;
        if (array[middle] == value)
        {
            return middle;
        }
        else if (array[middle] > value)
        {
            right = middle - 1;
        }
        else
        {
            left = middle + 1;
        }
    }
    return -1;
}

【问题讨论】:

  • 我无法编译图像。
  • 数组索引从 0 开始。
  • 如果它真的不打印任何东西(甚至是“升序:”),那么你就不要调用它,或者它是 windows 而不是控制台应用程序。
  • 它会打印所有数字,但是当用户输入数字进行搜索时,它不会打印任何内容。
  • 您好,请清理您的代码示例的缩进。

标签: c++ arrays sorting binary-search


【解决方案1】:

您的代码中几乎没有错误。检查这个。只为 10 个数字执行此操作。通过编写随机数逻辑将其设为 1000 或任何您喜欢的值。

#include <iostream>
int f10(int arr[],int size);
using namespace std;
int main()
{
    int arr[10] = {22,53,14,11,75,6,7,1,8,88};
    int result_index;
    result_index = f10(arr,10);
    cout << "Result = "<<result_index<<endl;    
}
int f10(int array[], int size) {
    int temp; // temporary swap variable 
    cout << "Default Array:" << endl;
    for (int x=0; x < size; x++)
    {
        cout<< array[x]<<", ";
    }
    cout << "Ascending Order:" << endl;
    for (int x = 0; x < size; x++){  

        for(int j = 0; j < size; j++) {

            if(j != size-1 && array[j] > array[j+1]) {  // Major Change

                // Swapping
                temp = array[j]; //temp is holding the first value 
                array[j] = array[j+1]; //
                array[j+1] = temp; //temp holds the second value
            }
        }      
    }
    for (int x=0; x < size; x++)
    {
        cout<< array[x]<<", ";
    }

    int value;
    cout <<"\nGimme a number to search:\n\t";
    cin >> value;

    int left = 0, right = 9; // Major change
    while (left <= right){ // Major change

        int middle = (left + right) / 2;
        if (array[middle] == value)
        {
            return middle;
        }
        else if (array[middle] > value)
        {
            right = middle - 1;
        }
        else
        {
            left = middle + 1;
        }
    }
    return -1;

}

【讨论】:

    猜你喜欢
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 2021-08-07
    • 2013-04-01
    • 2019-02-20
    • 1970-01-01
    相关资源
    最近更新 更多