【问题标题】:Recursive function for a binary search二分查找的递归函数
【发布时间】:2011-02-08 12:53:56
【问题描述】:

为二分搜索创建一个递归函数。
此函数接受排序后的数组和要搜索的项目,并返回项目的索引(如果项目在数组中),或者返回 -1(如果项目不在数组中)。
此外,编写一个测试程序来测试您的功能。

template <class elemType>
int orderedArrayListType<elemType>::binarysearch
                                (const elemType& item) const
{
    int first= 0;
    int last = length -1;
    int mid;
    int list[];
    int BinarySearch(,Type & Item, int first, int last)
    bool found = false;
    while (first <= last && !found){
        mid = (first + last) / 2;
        if (list[mid] > item)
            return BinarySearch(list, item, first, mid -1)
        found = true;
        else if (list[mid] > item)
            return BinarySearch( list, item, first, mid -1)
            last = mid - 1;
        else 
            first = mid + 1;
    }
    if (found)
        return mid;
    else 
        return -1;
}

【问题讨论】:

  • 这里的交易是您编写自己的作业解决方案,向我们询问您在代码中遇到的具体问题。
  • 一定是一年中的那个时候。
  • 导师不知道,还是不想给你做作业?
  • 请您的老师用您通常交流的任何语言解释问题,然后为我们翻译。
  • 我会说英语,她不会哈哈,但我重新编辑了我的问题并发布了我到目前为止所做的事情,感谢您在本网站上对新手的帮助

标签: c++ recursion binary-search


【解决方案1】:

在美国有一个儿童游戏,一个孩子选择 1 到 10 之间的数字,另一个孩子必须猜这个数字。如果他们猜错了,第一个孩子会说“更高”或“更低”。

大多数孩子一开始是随机猜测的,平均要尝试 4-5 次才能成功。我意识到(这可能就是我最终选择计算机科学的原因),最好的办法是选择中间点(5.5,所以选择 5 或 6。我会选择 5。)。根据他们所说的(“更高”或“更低”),选择一个新的范围,1-4 或 6-10。选择该范围中间的数字(2 或 8)。继续将范围分成两半,直到你得到数字。

这是对排序数组(排序数组是从 1 到 10 的数字)的二进制搜索。

要在代码中实现它,只需继续执行上述相同的过程。选择范围的中点,并根据答案创建一个新范围。

这里是 one solution in Java,它递归地执行此操作:

public class BinarySearchRecursive
{
    public static final int NOT_FOUND = -1;

    /**
     * Performs the standard binary search
     * using two comparisons per level.
     * This is a driver that calls the recursive method.
     * @return index where item is found or NOT_FOUND if not found.
     */
    public static int binarySearch( Comparable [ ] a, Comparable x )
    {
        return binarySearch( a, x, 0, a.length -1 );
    }

    /**
     * Hidden recursive routine.
     */
    private static int binarySearch( Comparable [ ] a, Comparable x,
                                     int low, int high )
    {
        if( low > high )
            return NOT_FOUND;

        int mid = ( low + high ) / 2;

        if( a[ mid ].compareTo( x ) < 0 )
            return binarySearch( a, x, mid + 1, high );
        else if( a[ mid ].compareTo( x ) > 0 )
            return binarySearch( a, x, low, mid - 1 );
        else
            return mid;
    }

    // Test program
    public static void main( String [ ] args )
    {
        int SIZE = 8;
        Comparable [ ] a = new Integer [ SIZE ];


    for( int i = 0; i < SIZE; i++ )
        a[ i ] = new Integer( i * 2 );

    for( int i = 0; i < SIZE * 2; i++ )
        System.out.println( "Found " + i + " at " +
                                 binarySearch( a, new Integer( i ) ) );
    }
}

【讨论】:

    【解决方案2】:

    你也可以google“递归二分搜索”和voila

    EDIT- 维基百科无所不知(尤其是涉及到 cs 时):

    最直接的 [二分搜索算法的]实现是递归的,它 递归搜索子范围 由比较决定:

       BinarySearch(A[0..N-1], value, low, high) {
           if (high < low)
               return -1 // not found
           mid = low + ((high - low) / 2) 
           if (A[mid] > value)
               return BinarySearch(A, value, low, mid-1)
           else if (A[mid] < value)
               return BinarySearch(A, value, mid+1, high)
           else
               return mid // found
       }
    

    【讨论】:

    • 不,他也不知道 =)。我已经在这件事上工作了一个星期,但我们还没有完成二进制搜索,但正如我所说,我们现在正在检查它们,所以我希望能得到一些提示,让我知道我应该做什么
    【解决方案3】:

    只需使用std::binary_search。告诉导师这个函数实际上是在your_favorite_compiler中递归实现的。

    【讨论】:

    • -1,这个答案是故意迟钝的。导师设定任务是为了促进学习,而不是试图让学生替换图书馆的功能。也许不应该教数学学生任何可以在计算器上完成的东西?
    • @JBentley - 最初形式的问题也是如此。无论如何,了解库函数并没有什么坏处。
    猜你喜欢
    • 2020-11-13
    • 2015-12-02
    • 1970-01-01
    • 2016-08-09
    • 2015-12-16
    • 2021-04-08
    • 1970-01-01
    • 2016-07-24
    • 2012-12-19
    相关资源
    最近更新 更多