我的二进制搜索算法模板函数总是返回错误

时间:2015-11-20 11:37:12

标签: c++ arrays algorithm binary-search readfile

所以我应该使用二进制搜索模板函数将文件中的元素读取到数组中,然后允许用户搜索元素(如果它在数组中)。问题是每当我搜索一个数字时,它就会给我一个“未找到”,即使该元素确实存在于文件中。我知道最好将模板函数保留在头文件中,但由于我不知道如何对文件进行排序以便二进制搜索能够工作,所以我将这些函数放在主程序中以减少它的混乱。认为问题出在main()或sort函数中,但据我所知,我无法弄清楚究竟在哪里以及如何修复它。

这是我的代码:

#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>

using namespace std;

template<class elemType>
class orderedArrayListType
{
public:
    static const int length = 20;//Const length of array you can change it accordingly
    int list[length];
    int binarySearch(elemType const&)const;
};

template<class elemType>
int orderedArrayListType<elemType>::binarySearch(const elemType& item) const
{
    int first = 0;
    int last = length - 1;
    int mid;

    bool found = false;

    while (first <= last && !found)
    {
        mid = (first + last) / 2;

        if (list[mid] == item)
            found = true;
        else if (list[mid] > item)
            last = mid - 1;
        else
            first = mid + 1;
    }

    if (found)
        return mid;
    else
        return -1;
}

void main()
{
    std::fstream numberFile("text.txt", std::ios_base::in);

    orderedArrayListType<int> object;

    int number=0, a;
    int i = 0;
    int numberToSearch;

    while (numberFile >> a)
    {
        object.list[i] = number;//Initalizing the array
        i++;
    }

    cout << "Enter Number you want to search" << endl;
    cin >> numberToSearch;

    int output = object.binarySearch(numberToSearch);//Make search

    if (output>0)
    {
        cout << "Element found at Index: " << output << endl;
    }
    else
    {
        cout << "Element not Found" << endl;
    }



}

这些是text.txt文件的内容:

  

1 2 3 4 5 6 7 8 9 10

提前致谢!

3 个答案:

答案 0 :(得分:0)

您可以在此处将列表的所有元素设置为0

while (numberFile >> a)
{
    object.list[i] = number;//Initalizing the array
    i++;
}

相反,您应该填写从文件中读取的数字:

while (numberFile >> a)
{
    object.list[i] = a;//Initalizing the array
    i++;
}

然后,如果您将模板参数用于列表会更好,因为否则该模板仅适用于int

template<typename elemType>
class orderedArrayListType
{
public:
    static const int length = 20;//Const length of array you can change it accordingly
    elemType list[length];
    int binarySearch(elemType const&)const;
};

template<typename elemType>
int orderedArrayListType<elemType>::binarySearch(const elemType& item) const
{
    int first = 0;
    int last = length - 1;
    elemType mid;
    ....

答案 1 :(得分:0)

问题不在于二元搜索功能,而是:

 while (numberFile >> a)
 {
    object.list[i] = number;//Initalizing the array
    i++;
 }

number始终为0.

一个建议,改变:

mid = (first + last) / 2;

mid=first + (last-first)/2 

避免溢出。寻找这些案例总是一个很好的编程实践。

答案 2 :(得分:0)

感谢这些提示,我能够正确运行程序。这是编辑过的main()程序代码,可以让任何可能偶然发现此类程序出现问题的人更清楚。

void main()
{
    std::fstream numberFile("text.txt", std::ios_base::in);

    orderedArrayListType<int> object;

    int number=0, a;
    int i = 0;
    int numberToSearch;

    while (numberFile >> a)
    {
        object.list[i] = a;//Initalizing the array
        i++;
    }

    cout << "Enter Number you want to search" << endl;
    cin >> numberToSearch;

    int output = object.binarySearch(numberToSearch);//Make search

    if (output>0)
    {
        cout << "Element found at Index: " << output << endl;
    }
    else
    {
        cout << "Element not Found" << endl;
    }



}

请注意,仅对用户(alain)建议初始化数组的部分进行了更改:

while (numberFile >> a)
    {
        object.list[i] = a;//Initalizing the array
        i++;
    }