0

我正在尝试binarySerach递归实现,但我遇到了堆栈溢出。

首先,我有find一个需要 3 个参数的函数。然后我调用这个函数main并从数组中传递地址。坚果我有堆栈溢出!

bool find( const int x, const int* pBegin, const int* pEnd)
{    
    int medel =((*pBegin) + (*pEnd -1))/2 ;

    if(x == medel)
        return true ;

    else if( x<medel)
    {
        int last = medel -1 ;
        return find(x, pBegin, &last);
    }
    else if( x > medel) 
    {
        int begin = medel +1;
        return find(x, &begin, pEnd);
    }


}// find

void main()
{    
    int arr[10];
    for (int i=0;i<10;++i)
        arr[i] = i;
    bool found = find(3, &arr[0], &arr[10]);
    cout << "hittat = " << found << endl;

    system("pause");
}
4

1 回答 1

0

There are several very fundamental issues around the use of pointers in your code. Without fixing the code for you, I'll give you a hint:

Try turning medel into a pointer to the middle element. This might help resolve some of the confusion.

于 2013-01-26T15:18:37.427 回答