<source id="v77gz"></source>
    <u id="v77gz"><p id="v77gz"></p></u>

      <samp id="v77gz"></samp>

      <var id="v77gz"><td id="v77gz"><ins id="v77gz"></ins></td></var>

      <video id="v77gz"></video>
      <source id="v77gz"></source>
    1. 江蘇省高校計算機等級考試命題研究院 江蘇省高校計算機等級考試輔導
      江蘇省VB常用算法(八)查找- -

      VB常用算法(八)查找- -

      曹蘇群  http://caosuqun.bokee.com

      Tag查找    算法                                          

      1、算法說明

      1)        順序查找

               逐個元素找,如果有,則記錄位置,然后跳出循環;否則,查找失敗。

               代碼如下:

                        Private Sub Search(a(), ByVal Key, Index As Integer)

                              Dim i%

                              For i = LBound(a) To UBound(a)

                                    If a(i) = Key Then '找到,將元素下標保存在index中并結束查找

                                             Index = i

                                             Exit Sub

                                    End If

                              Next i

                              Index = -1             '若沒找到,則index值為-1

                        End Sub

       

      2)        二分法查找

      順序查找效率低下,當數組有序排列時,可以使用二分法查找提高效率。

               算法思想:

      代碼如下:

                        Private Sub birSearch(a(), ByVal low%, ByVal high%, ByVal Key, index%)

                              Dim mid As Integer

                              If low > high Then

           '沒有查找到

                                    index = -1

                                    Exit Sub

                              End If

                              mid = (low + high) \ 2 '取查找區間的中點

                              If Key = a(mid) Then   '查找到,返回下標

                                    index = mid

                                    Exit Sub

                              ElseIf Key < a(mid) Then '查找區間在上半部分

                                high = mid - 1

                           Else

                                low = mid + 1    '查找區間在下半部分

                              End If

                              Call birSearch(a, low, high, Key, index) '遞歸調用查找函數

                        End Sub

      調用方法:

                        Private Sub Command1_Click()

                              Dim a(11)

                              a(1) = 5: a(2) = 13: a(3) = 19: a(4) = 21: a(5) = 37

                              a(6) = 56: a(7) = 64: a(8) = 75: a(9) = 80: a(10) = 88: a(11) = 92

                              Dim ind As Integer

                              Call birSearch(a, LBound(a), UBound(a), 21, ind)

                              Print ind

                        End Sub

      2、實戰練習

               補充代碼(2002春二(9))

               C盤根目錄下stu.txt文件中以下的格式保存著學生姓名及班號信息。本程序的功能是:讀取該文件中的數據,再利用InputvBox輸入要查找的學生姓名,通過順序查找,給出找到或找不到的信息。附圖是某同學被找到時顯示的信息窗口。

              張文,機械01

              李明,機械01

              王文卉,機械01

             

              何宇宙,電子02

       

                        Option Explicit

                        Option Base 1

                        Private Sub Form_click()

                                 Dim stu() As String, glass() As String, i As Integer

                                 Dim n As Integer, name As String, k As Integer

                                 Open "c:\stu\stu.txt" For Input As #11

                                 Do While 1

                                          2

                                          ReDim Preserve stu(n), glass(n)

                                          Input #11, stu(n), glass(n)

                                 Loop

                                 name = InputBox("輸入欲查找的學生姓名:")

                                 Call search(name, stu, k)

                                 If k <= n Then

                                          MsgBox 3

                                 Else

                                          MsgBox "無此人。"

                                 End If

                                 Close #11

                        End Sub

                        Private Sub search(name As String, stu() As String, k As Integer)

                                 Dim i As Integer

                                 For i = 1 To UBound(stu)

                                          If name = stu(i) Then

                                                   4

                                          End If

                                 Next i

                                 5

                        End Sub

      中文在线字幕第一页_中文字幕高跟丝袜作品番号_亚洲中文无无码第_日本亚洲欧美国产日韩a??y_在线观看亚洲av无码专区