二进制搜索 - 代码编译并运行后不显示输出

Binary Search - Output is not displaying after code compiles and runs

本文关键字:显示 输出 运行 搜索 代码 编译 二进制      更新时间:2023-10-16

我现在刚刚在课堂上学习二进制搜索,下面的代码只是一个示例,因为我试图更好地理解它。所以话虽如此,这段代码正在编译但不显示任何输出,并且由于我对二进制搜索缺乏了解,我不知道为什么没有任何输出。有人可以指出写得很好的教程的方向吗?或者帮助指出代码出了什么问题。

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <vector>
#include <algorithm>
using namespace std;
int SIZE = 10;
int main()
{
    int thisArray[] = { 99,86,44,55,78,63,0,32,11 };
    int num = 0;
    int n = 0;
    int first;
    int last;
    int middle;
    first = 0;
    last = n - 1;
    middle = (first + last) / 2;
    cout << "Enter the total number of elementsn";
    cin >> n;
    cout << "Entered " << n << "number.n";
    for (int i = 0; i < n; i++) {
        cin >> thisArray[i];
    }
    cout << "Enter a number to find.n";
    cin >> num;
    while (first <= last) {
        if (thisArray[middle] < num) {
            first = middle + 1;
        }
        else if (thisArray[middle] == num ) {
            cout << num << " found at location " << middle + 1 << "n";
            break;
        }
        else {
            last = middle - 1;
        }
        middle = (first + last) / 2;
    }

    return 0;
}

编辑:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <vector>
#include <algorithm>
using namespace std;
int SIZE = 10;
int main()
{
    //this is my binary search
    int thisArray[10] = { 0,11,32,44,55,63,78,86,99 };
    int i = 0; //index of the array
    int n = 0; //variable of number that will be looked for
    int first = 0;
    int last = SIZE - 1;
    int middle;
    int pos = -1;
    bool found = false;
    int count = 0;
    while (found) {
        cout << "Enter a number to look for.n";
        cin >> n;

        while (first <= last) {
            middle = first + last / 2;
            if (thisArray[middle] == n) {
                pos = middle;
                cout << "item found at " << middle + 1 << "n";
                exit(0);
            }
            else if (thisArray[middle] > n) {
                last = middle - 1;
            }
            else {
                first = middle + 1;
            }//endif
        }//end while
    }//end big while
    //if()

    return 0;
}

知道了。感谢大家的帮助!

它没有输出任何东西,因为first == 0last == -1.因此,first <= last永远不会为真,循环体也永远不会被执行。

你知道吗? 如果数组没有正确排序以使用二叉搜索属性,您怎么能使用二叉搜索......

请停止像杰克·米格那样回答问题,我讨厌没有直接解决问题的答案,它有点重述问题......

简单

last = n - 1;更改为last = SIZE - 1;

或者在接受n值后计算last = n - 1;

数组也需要排序!

通了。现在,虽然代码是一个例子,但我发现 for 循环对我来说无法正常工作,所以我摆脱了它。对于迭代,我实现了一个do-while循环。但是,代码现在可以工作了。

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <vector>
#include <algorithm>
using namespace std;
int SIZE = 10;
int main()
{
    //this is my binary search
    int thisArray[10] = { 0,11,32,44,55,63,78,86,99 };
    int i = 0; //index of the array
    int n = 0; //variable of number that will be looked for
    int first = 0;
    int last = SIZE - 1;
    int middle;
    int pos = -1;
    bool found = false;
    int count = 0;
    do {
        cout << "Enter a number to look for.n";
        cin >> n;

        while (first <= last) {
            middle = first + last / 2;
            if (thisArray[middle] == n) {
                pos = middle;
                cout << "item found at " << middle + 1;
                exit(0);
            }
            else if (thisArray[middle] > n) {
                last = middle - 1;
            }
            else {
                first = middle + 1;
            }//endif
        }//end while
    } while (found = true);
    return 0;
}