Module Module1

    Sub Main()
        Dim array(999) As Integer
        Dim searchValue As Integer
        Dim valueIndex As Integer
        Dim rnd As New Random()

        For i As Integer = 0 To UBound(array)
            If (i = 0) Then
                array(i) = rnd.Next(10)
            Else
                array(i) = rnd.Next(array(i - 1), array(i - 1) + 10)
            End If
            Console.WriteLine("array(" & i & ")=" & array(i))
        Next i

        Console.WriteLine(vbCrLf & "请输入一个1~100之间的整数:")
        searchValue = Console.ReadLine()

        valueIndex = Find(array, searchValue)
        If (valueIndex <> -1) Then
            Console.WriteLine("查找成功!元素位于索引(" & valueIndex & ")处")
        Else
            Console.WriteLine("查找失败!")
        End If

        Console.ReadKey()
    End Sub

    Function Find(ByVal array() As Integer, ByVal searchValue As Integer) As Integer
        Dim result As Boolean
        Dim valueIndex As Integer
        Dim startIndex, endIndex As Integer

        startIndex = 0
        endIndex = UBound(array)
        result = BinarySearch(array, searchValue, startIndex, endIndex, valueIndex)

        Return IIf(result, valueIndex, -1)
    End Function

    Function BinarySearch(ByVal array() As Integer, ByVal searchValue As Integer, ByRef startIndex As Integer, ByRef endIndex As Integer, ByRef valueIndex As Integer) As Boolean
        If (array Is Nothing Or array.Length = 0) Then
            Return False
        End If

        Dim midIndex As Integer
        midIndex = Math.Ceiling((startIndex + endIndex) / 2)
        If (array(midIndex) = searchValue) Then
            valueIndex = midIndex
            Return True
        End If

        If (array(midIndex) < searchValue) Then
            startIndex = midIndex + 1
            endIndex = endIndex
        Else
            startIndex = startIndex
            endIndex = midIndex - 1
        End If

        Return BinarySearch(array, searchValue, startIndex, endIndex, valueIndex)
    End Function
End Module

二分查找 - vb.net的更多相关文章

  1. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

  2. Java实现的二分查找算法

    二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...

  3. 从一个NOI题目再学习二分查找。

    二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...

  4. java实现二分查找

    /** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...

  5. 最新IP地址数据库 二分逼近&二分查找 高效解析800万大数据之区域分布

    最新IP地址数据库  来自 qqzeng.com 利用二分逼近法(bisection method) ,每秒300多万, 比较高效! 原来的顺序查找算法 效率比较低 readonly string i ...

  6. c#-二分查找-算法

    折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小 ...

  7. 【Python】二分查找算法

    二分查找:在一段数字内,找到中间值,判断要找的值和中间值大小的比较.如果中间值大一些,则在中间值的左侧区域继续按照上述方式查找.如果中间值小一些,则在中间值的右侧区域继续按照上述方式查找.直到找到我们 ...

  8. PHP实现文本快速查找 - 二分查找

    PHP实现文本快速查找 - 二分查找法 起因 先说说事情的起因,最近在分析数据时经常遇到一种场景,代码需要频繁的读某一张数据库的表,比如根据地区ID获取地区名称.根据网站分类ID获取分类名称.根据关键 ...

  9. java二分查找举例讨论

    最近做笔试题有这么一个关于二分查找的例子. 给一个有序数组,和一个查找目标,用二分查找找出目标所在index,如果不存在,则返回-1-(其应该出现的位置),比如在0,6,9,15,18中找15,返回3 ...

随机推荐

  1. C#+QI的例子

    COM中,和我们打交道的是接口,也就是说类对我们是隐形的,那么我们要做开发,要使用这些功能,我们只能通过接口,通过接口暴露出来的方法,COM是一种服务器端/客户端架构,服务器端定义了操作的法,客户端通 ...

  2. PAT (Advanced Level) 1025. PAT Ranking (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  3. cnn 文章

    http://www.cnblogs.com/fengfenggirl/p/cnn_implement.html http://www.2cto.com/kf/201603/493553.html h ...

  4. Shell条件与测试

    分类参考 文件状态测试 -b filename 当filename 存在并且是块文件时返回真(返回0) -c filename 当filename 存在并且是字符文件时返回真 -d pathname ...

  5. MySQL的mysql_insert_id和LAST_INSERT_ID

    摘要:mysql_insert_id和LAST_INSERT_ID二者作用一样,均是返回最后插入值的ID 值 1 mysql_insert_id 一.PHP获取MYSQL新插入数据的ID  mysql ...

  6. Grunt 入门

    转自:http://user.qzone.qq.com/174629171/blog/1404433906 Grunt被定义为:the javascript task runner. 什么算是Java ...

  7. object - c 在URL中截取特定参数的值

    #pragma mark - 获取url特定的参数 -(NSString *) jiexi:(NSString *)CS webaddress:(NSString *)webaddress { NSE ...

  8. 严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called for this response

    严重: Servlet.service() for servlet jsp threw exception    java.lang.IllegalStateException: getOutputS ...

  9. 【转】23种设计模式UML图

    原文:http://blog.csdn.net/bwwlpnn/article/details/7421628

  10. Memcached源码分析之slabs.c

    #include "memcached.h" #include <sys/stat.h> #include <sys/socket.h> #include ...