1

Is it possible to divide an Array?

Example:

array(2) As String
array(1) = "test1"
array(2) = "test2"

~ Now Split

array1 (contains test1) & array 2 (contains test2)

I want to implement a Binarysearch

4

3 回答 3

0

你可以这样拆分

Sub split_array()

Dim array1(1 To 2) As String
Dim array2(1 To 2) As String
Dim array3(1 To 2) As String

array1(1) = "Test1"
array1(2) = "Test2"

array2(1) = array1(1)
array3(1) = array1(2)

End Sub

但我怀疑这不是最好的方法。我认为使用 3 个(可能是长整数)变量来表示数组中的位置会做得更好。1 表示第一个元素,1 表示最后一个元素,1 表示中间元素。

Dim lLowerSearchElement As Long
Dim lUpperSearchElement As Long
Dim lMiddleSearchElement As Long
Dim array1(1 to 999) as string

lLowerSearchElement = 1
lUpperSearchElement = 999
lMiddleSearchElement = (lUpperSearchElement + lLowerSearchElement) / 2

然后,您可以检查元素是否等于、大于或小于中间元素并相应地继续。

还要记住,在尝试使用二进制搜索之前,您需要对数据进行排序,如果您了解递归调用,这将很有用。

您还需要严格测试您的实现,因为一个小错误可能会导致搜索无法正常工作。

编辑 22/08/13

下面给出了我用于二进制搜索的实现:

Function bCheckSamplePoint(ByRef lSamplePointArray() As String, ByRef bfound As Boolean, _
    ByVal lSamplePoint As String) As Boolean
    'byref used for the array as could be slow to keep copying the array, bFound is used by calling procedure

    Dim lLowerSearchElement As Long
    Dim lUpperSearchElement As Long
    Dim lMiddleSearchElement As Long

    bfound = False 'False until found

    'Set initial limits of the search
    lLowerSearchElement = 0
    lUpperSearchElement = UBound(lSamplePointArray())

    Do While lLowerSearchElement <= lUpperSearchElement And bfound = False
        lMiddleSearchElement = (lUpperSearchElement + lLowerSearchElement) / 2
        If StrComp(lSamplePointArray(lMiddleSearchElement), lSamplePoint, vbTextCompare) = -1 Then
'            'Must be greater than middle element
            lLowerSearchElement = lMiddleSearchElement + 1
        ElseIf (lSamplePointArray(lMiddleSearchElement) = lSamplePoint) Then
            bfound = True
        Else
            'must be lower than middle element
            lUpperSearchElement = lMiddleSearchElement - 1
        End If 'lSamplePointArray(lmiddlesearchlelemnt) < lSamplePoint

    Loop 'While lLowerSearchElement <= lUpperSearchElement

ErrorExit:
    bCheckSamplePoint = bReturn
    Exit Function

正如你所看到的,这个二分搜索只是检查是否在一个字符串数组中找到了一个字符串,但它可以被修改用于其他目的。

于 2013-08-21T17:57:13.170 回答
0

您不需要拆分函数来进行二分搜索

我的 VBA 版本的二分搜索可以在
http://fastexcel.wordpress.com/2011/08/02/developing-faster-lookups-part-3-a-binary-search-udf/找到

于 2013-08-21T18:26:24.657 回答
0

将数组拆分成块

Public Function splitArray(ByVal initial_array As Variant, Optional chunk_size As Long = 1) As Variant()
  Dim split_array() As Variant
  Dim chunk() As Variant
  Dim chunk_index As Integer: chunk_index = 0
  Dim array_index As Integer: array_index = 1

  If UBound(initial_array) > chunk_size Then
    For i = 0 To UBound(initial_array)
      If (i + 1) / (chunk_size * array_index) = 1 Or i = UBound(initial_array) Then
        ReDim Preserve chunk(chunk_index)
        chunk(chunk_index) = initial_array(i)
        ReDim Preserve split_array(array_index - 1)
        split_array(array_index - 1) = chunk
        chunk_index = 0
        array_index = array_index + 1
      Else
        ReDim Preserve chunk(chunk_index)
        chunk(chunk_index) = initial_array(i)
        chunk_index = chunk_index + 1
      End If
    Next i
    splitArray = split_array
  Else
    ReDim Preserve split_array(0)
    split_array(0) = initial_array
    splitArray = split_array
  End If

End Function
于 2019-02-09T20:16:45.643 回答