[Visual Basic] binary search

template

i=1:j=n              'i初值为1,j初值为n
Do while i<=j        '当i<=j时,通过循环进行查找
    m=fix((i+j)/2)     '计算出中间元素的下标m
    If d(m)=key Then '若中间元素d(m)和key相等,则输出m;退出循环
        print m
        Exit Do
    End If
    If d(m)<key Then 
        i=m+1         '[m+1,j]下一查找范围
    Else
        j=m-1         '[i,m-1]
    End If
Loop
If i>j Then print"0"   '未找到

Precautions and Variant

  • Find times: LogN (if not an integer, it is rounded up)

  • Under the subject of computing intermediate element there are many, is the choice of a small pit (I fell in many times)

'e.g.
'偶数组 1 2 3 4 5 6 7 8
'奇数组 1 2 3 4 5 6 7 8 9
'偶数组 i=1:j=8  奇数组 i=1;j=9
'                      偶数组    奇数组                   备注
① Int((i+j)/2)    '      4        4           删除小数部分而返回剩下的整数
② Fix((i+j)/2)    '      4        4        
③ (i+j)/2         '     4.5       5          Dim c as integer c=(i+j)/2=4
④ (i+j)\2         '      4        5                 选择左数或中间数
⑤ (i+j+1)/2       '      5        5                 选择右数或中间数
⑥ Fix((i+j)/2 +0.5)' 

General topics, mostly from the left and select the number.
Also see the array number is zero or 1 began in high school where papers are more than 1 start

  • Binomial tree

qwq picture is from teacher to courseware in white prostitute da (slide
Example:
the following VB program segment:

i = 1 : j = 10 : flag = True : n = 0 
Key = Val(Text1.Text)
Do While i <= j And flag = True
   m = (i + j) \ 2
   If a(m) = Key Then
      flag = False
   Elseif a(m) > Key Then
       i = m + 1 : n = n - 1
   Else
      j = m - 1 : n = n + 1
   End If
Loop

Array element a (1) to a (10) followed by 99, 94, 90, 87, 70, 69, 60, 56, 45, 36, the value of the variable n is ultimately 0, the digital text box Text1 input may be
A. 100 B. 87 C. 69 D. 4
binary tree most obvious feature of solving problems is n = n-. 1; n = n +. 1 , in this question in the former means "When it is desired to find values Key smaller than two points, then right search, and n -1 "(data descending), the latter and vice versa.
n = number of indented left - right number of indentation
so following a simulated binary (number in blue, red to n)
TFT
is easy to see when n = 0, the number of check 69,70,90 . therefore, the answer to the title C
These are very basic questions naked, take a look at Precautions:

  1. Number Sequencing rise or fall? When n + 1 or n-1?
  2. When the number of programs to find whether to proceed with
    this question and adopted the Flag If, ElseIf, Else, Exit For there can be found to achieve the purpose of the program will stop. If the code is missing from the above statement, as two If not with Exit For, only the If and Else, the program continues, the value of n will still change, this time it will continue to draw a binary tree
  3. There are options and greater than a maximum value less than the minimum
    is the question of A100 and D4, it has been shown in FIG. Needs to spread out in the right-most original binary tree (only the number of columns) of the leftmost, specifically Figure.
  4. Variant: n seeking to meet the range of input data 0:00
    Answer: (Loading ......) this place and I still do not know ...... if you can come and teach me ......
It is thought of these difficulties, if later encountered a new subject to write, will continue to update. Welcome to rectify oh ~

Tidbits: ask the teacher topic screenshot write blog (lp holidays and certainly is the man went to the hum slightly abbreviated)
1
2

Guess you like

Origin www.cnblogs.com/lsqwq/p/BinarySearch.html