Performing a Binary Search through an Array : Array Sort « Data Type « VBA / Excel / Access / Word

Home
VBA / Excel / Access / Word
1.Access
2.Application
3.Data Type
4.Data Type Functions
5.Date Functions
6.Excel
7.File Path
8.Forms
9.Language Basics
10.Math Functions
11.Outlook
12.PowerPoint
13.String Functions
14.Windows API
15.Word
16.XML
VBA / Excel / Access / Word » Data Type » Array Sort 
Performing a Binary Search through an Array
 
  Option Explicit
  Option Base 1

  Sub Binary_Search_of_Array()
      Dim intThousand(1000As Integer
      Dim As Integer
      Dim intTop As Integer
      Dim intMiddle As Integer
      Dim intBottom As Integer
      Dim varUserNumber As Variant

      For i = To 1000
          intThousand(i= i
      Next i

      varUserNumber = 233
      intTop = UBound(intThousand)
      intBottom = LBound(intThousand)

      Do
          intMiddle = (intTop + intBottom2
          If varUserNumber > intThousand(intMiddleThen
             intBottom = intMiddle + 1
          Else
              intTop = intMiddle - 1
          End If
      Loop Until (varUserNumber = intThousand(intMiddle)) _
          Or (intBottom > intTop)

      If varUserNumber = intThousand(intMiddleThen
          Debug.Print varUserNumber & ", at position " & intMiddle 
      Else
          Debug.Print "not in "
      End If
  End Sub

 
Related examples in the same category
1.VBA Bubble Sort
2.using dynamic arrays in bubble sort
3.Quick sort
4.Quick Sort 2
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.