C ++哈希搜索功能陷入无尽的“ else”和“ while”循环中

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

如果散列表数组中不存在要查找的生成随机数,则程序将陷入函数void hashSearch()的无限循环中,而应该只是跳出循环并输出未找到搜索项的内容。代码在输出中的确切位置是:cout << "stuck in else loop \n";cout << "stuck in while loop end \n";

我在Google周围搜索,但找不到类似的示例。

#include <iostream>
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <chrono>
using namespace std;
int arr [1000];
int arr2 [1000];
int randArrayInt, n, randSearchItem, searchInt, address, size2;
void printZeroArr();
void linearSentinelSearch();
void printHashArray();
void hashSearch();
int main ()
{
    srand (time(nullptr));  //initialize random seed:
    n = rand() % 900 + 100; //random integer number from 100 - 1000, length of the array
    //n = rand() % 10; // random number in the range 1-10 for sanity tests, length of the array
    //randSearchItem = rand() % 10 + 1;
    randSearchItem = rand() % 900 + 100; //this is the number to search for
    cout << "Array length is " << n << endl;
    cout << "[";
    for (int i = 0; i <= n; i++)
    {
        randArrayInt = rand() % 900 + 100;
        //randArrayInt = rand() % 10 + 1; // generate random 1-10 number for for sanity tests
        arr[i] = randArrayInt;   // insert into array position the generated random number
        cout<< " " << arr[i];  // print out array element at current loop position
    }
    cout << " ]\n" << endl;
    printZeroArr();
}

void printZeroArr()
{
    size2 = n + 1; //length of hashed array
    cout << "This is the random key to search for in array: " << randSearchItem << endl;
    cout << "This is the size2 length " << size2 << endl;
    cout << "This is the hasharray with zeros" << endl;
    cout << "[";
    for (int i = 0; i <= size2; i++)
    {
        arr2[i] = 0;   // insert into hasharray number 0
        cout<< " " << arr2[i];  // print out hasharray element at current loop position
    }
    cout << " ]\n" << endl;
    linearSentinelSearch();
}

void linearSentinelSearch()
{
    auto start = std::chrono::high_resolution_clock::now();
    arr[n + 1] = randSearchItem;
    //cout << "testing arr[n + 1] is " << arr[n + 1] << endl;
    int i = 0;
    while (arr[i] != randSearchItem) i++;
    if (i == n + 1)
        cout << "Sentinel search did not found the searchitem in random array" << "\n" << endl;
    else
        cout << "Searchitem found in array with linearsearch at position " << i << "\n" << endl;
    auto finish = std::chrono::high_resolution_clock::now();
    chrono::duration<double> elapsed = finish - start;
    cout << "Elapsed time: " << elapsed.count() << " s\n";
    printHashArray();
}

void printHashArray()
{
    //cout << "printing out 'address' value, or the modulo result: " << endl;
    //cout << "[";
    for (int i = 0; i <= n; i++)
    {
        address = arr[i] % size2;
        //cout << " " << address;
        while (arr2[address] != 0)
        {
            if (address == size2 - 1)
            {
                address = 0;
            } else
            {
                address++;
            }
        }
        arr2[address] = arr[i];
    }
    //cout << " ]\n" << endl;
    cout << "This is the hasharray with hashitems" << endl;
    cout << "[";
    for (int i = 0; i <= size2; i++)
    {
        cout << " " << arr2[i];
    }
    cout << " ]\n" << endl; hashSearch();
}

void hashSearch()
{
    auto start = std::chrono::high_resolution_clock::now();
    int searchInt = randSearchItem % size2;
    while ((arr2[searchInt] != 0)  && (arr2[searchInt] != randSearchItem))
    {
        if (searchInt == size2 - 1)
        {
            searchInt = 0;
            cout << "if loop \n";
        }
        else
        {
            searchInt++;
            cout << " stuck in else loop \n";
        }
        cout << " stuck in while loop end \n";
    }
    if (searchInt == 0) {
        cout << "Search item not found using hashSearch" << endl;
    } else {
        cout << "Search item " << randSearchItem << " found using hashSearch at position " << searchInt << " in arr2." << endl;
    }
    auto finish = std::chrono::high_resolution_clock::now();
    chrono::duration<double> elapsed = finish - start;
    cout << "Elapsed time: " << elapsed.count() << " s\n";
}

而它应该跳出循环并输出未找到搜索项。搜索cout << " stuck in else loop \n";cout << " stuck in while loop end \n";

c++ algorithm hashtable
1个回答
0
投票

您想在到达数组末尾时停止循环:为此,将要搜索的项目设置为零:

    if (searchInt == size2 - 1)
    {
        searchInt = 0;
        cout << "if loop \n";
    }

但是在循环控件中,您无需对此进行测试。您只测试当前索引处的数组元素是否为零(找不到)或要搜索的项目(找到):

while ((arr2[searchInt] != 0)  && (arr2[searchInt] != randSearchItem)) ...

您需要其他测试:

while ((searchInt != 0)  && ...) ...

花了我一段时间,您想对一个开放地址的hastable进行编码,其中的零标记未使用的插槽。哈希值就是数字本身。用零作为空槽的指示器不是理想的:您不能存储哈希码以表大小为模的数字。

我还将使用非空函数进行编码,其中返回值是索引或某些明确的值,表示“未找到”,也许为-1。 (或者,您可以返回指向找到的项目的指针,如果找不到该项目,则可以返回NULL-毕竟,哈希数组中的索引是哈希表内部的一部分,而与调用者无关。) >

然后您可以使用早期回报:

int hashSearch(const int *arr2, int size2, int item)
{
    int i = item % size2;

    for (; i < size2; i++) {
        if (arr2[i] == -1) break;            // -1 indicated unused space
        if (arr2[i] == item) return i;       // return index of item
    }

    return -1;     // not found!
}

正如已经在注释中向您指出的:尝试使用函数参数和局部变量,而不是将所有内容放到全局空间中。同样,函数调用的链是奇怪的,其中函数中的最后一件事是调用下一个。将所有顺序调用放入main可能更好。

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