台检查二进制搜索 [英] Desk Check Binary Search

查看:154
本文介绍了台检查二进制搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有关我面对的查询。

我想知道我如何会去检查桌子下面code。

数据集

  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 154 7 19 25 36 37 50 100 101 205 220 271 306 321 456 500 / *数字是有点搞砸* /

算法
的binarySearch

  SET发现FALSE
SET底部为零
SET顶到sizeOfList-1
WHILE(未找到和底部< =顶部)
    SET中间(下+上)DIV 2
    IF searchValue<列表元素中间THEN
        SET顶到中间1
    其他
        IF searchValue>列表元素中间THEN
            SET底部中等+ 1
        其他
            设定位置为中间
            SET发现TRUE
        万一
    万一
ENDWHILE如果没有,那么找到
    返回-1
其他
    返回位置
万一


解决方案

要做到这一点是先画一个表,对每个变量(中发现一列的最佳方式等)。那么,是的计算机,请通过程序code步骤的一行时间(可能是最好写下您访问跟踪每个行号),同时根据您的表中的值的条件分支。每次修改一个变量,添加一个新行到表的更新值。最终,你应该达到一个收益语句,然后就大功告成了。

I am here about a query that I am facing.

I was wondering on how I would go about desk checking the following code.

Data set

0 1  2 3  4   5  6  7   8   9  10  11   12  13  14 15

4 7 19 25 36 37 50 100 101 205 220 271 306 321 456 500 /* Numbers are a bit messed up */

Algorithm binarySearch

SET found TO FALSE
SET bottom TO zero
SET top TO sizeOfList-1
WHILE ( NOT found AND bottom <= top )
    SET middle TO (bottom+top) DIV 2
    IF searchValue < list element middle THEN
        SET top TO middle-1
    ELSE
        IF searchValue > list element middle THEN
            SET bottom TO middle+1
        ELSE
            SET position TO middle
            SET found TO TRUE
        ENDIF
    ENDIF
ENDWHILE

IF NOT found THEN
    RETURN –1
ELSE
    RETURN position
ENDIF

解决方案

The best way to do this is to first draw up a table, with one column for each variable (found, bottom, top, etc.). Then, "be" the computer, step through your program code one line at a time (probably best to write down each line number that you visit to keep track), taking the conditional branches based on the values in your table. Every time you modify a variable, add a new row to your table with the updated values. Eventually, you should reach a return statement, and then you're done.

这篇关于台检查二进制搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆