我如何通过VBA中的属性快速排序对象数组



当尝试通过属性对对象进行排序时,我在VBA中遇到了快速排序算法的问题。泡沫排序很好。我认为这与对象引用有关,但我似乎搞不清楚。在我为对象添加了一个clone方法之前,bubblessort过去也会失败。

下面是相关支持代码的粘贴包:http://pastebin.com/egcph0jJ .

这是相关的函数:

Private Sub quick_sort_file_array_by_modified(file_array As Variant, inLowBound As Long, inHighBound As Long)

  'Do a quicksort on date_modified
  Dim pivot   As ParsedFile
  Dim tmpSwap As ParsedFile
  Dim tmpLow  As Long
  Dim tmpHi   As Long
  tmpLow = inLowBound
  tmpHi = inHighBound
  Set pivot = file_array((inLowBound + inHighBound) / 2)
  While (tmpLow <= tmpHi)
    Dim tmp_low_file As ParsedFile
    Set tmp_low_file = file_array(tmpLow)
     While (tmp_low_file.file_mod_date < pivot.file_mod_date And tmpLow < inHighBound)
        tmpLow = tmpLow + 1
     Wend
     Dim tmp_high_file As ParsedFile
     Set tmp_high_file = file_array(tmpHi)
     While (pivot.file_mod_date < tmp_high_file.file_mod_date And tmpHi > inLowBound)
        tmpHi = tmpHi - 1
     Wend
     If (tmpLow <= tmpHi) Then
        Set tmpSwap = file_array(tmpLow)
        Set file_array(tmpLow) = file_array(tmpHi).clone
        Set file_array(tmpHi) = tmpSwap.clone
        tmpLow = tmpLow + 1
        tmpHi = tmpHi - 1
     End If
  Wend
  If (inLowBound < tmpHi) Then quick_sort_file_array_by_modified file_array, inLowBound, tmpHi
  If (tmpLow < inHighBound) Then quick_sort_file_array_by_modified file_array, tmpLow, inHighBound

End Sub

…结果是它根本没有改变对象数组的顺序。

你的快速排序算法是错误的。

While (tmp_low_file.file_mod_date < pivot.file_mod_date And tmpLow < inHighBound)
        tmpLow = tmpLow + 1
Wend

您只更新索引计数器,但不更新比较值"tmp_low_file。file_mod_date & lt;主。file_mod_date"总是保持不变,tmpLow也会直到inHighBound

运行此测试代码,您将看到错误所在。

Public Sub test()
    changedArr = Array(2, 1, 33, 89, 76, 10, 11)
    quick_sort_file_array_by_modified changedArr, 0, 6
End Sub
Private Sub quick_sort_file_array_by_modified(file_array As Variant, inLowBound As Long, inHighBound As Long)
    'Do a quicksort on date_modified
      Dim pivot   As Integer
      Dim tmpSwap As Integer
      Dim tmpLow  As Long
      Dim tmpHi   As Long
      Debug.Print "inLowBound: "; CStr(inLowBound) & "  inHighBound:" & CStr(inHighBound)
      PrintArr file_array
      tmpLow = inLowBound
      tmpHi = inHighBound
      pivot = file_array((inLowBound + inHighBound) / 2)
      Debug.Print "Pivot Value:" & pivot
      While (tmpLow <= tmpHi)
        Dim tmp_low_file As Integer
        tmp_low_file = file_array(tmpLow)
         While (tmp_low_file < pivot And tmpLow < inHighBound)
            tmpLow = tmpLow + 1
         Wend

         Dim tmp_high_file As Integer
         tmp_high_file = file_array(tmpHi)
         While (pivot < tmp_high_file And tmpHi > inLowBound)
            tmpHi = tmpHi - 1
         Wend
         If (tmpLow <= tmpHi) Then
            Debug.Print "Swaping: " & CStr(tmpLow) & " to "; CStr(tmpHi)
            tmpSwap = file_array(tmpLow)
            file_array(tmpLow) = file_array(tmpHi)
            file_array(tmpHi) = tmpSwap
            tmpLow = tmpLow + 1
            tmpHi = tmpHi - 1
         End If
         PrintArr file_array
      Wend
      Debug.Print "inLowBound: "; CStr(inLowBound) & "  tmpHi:" & CStr(tmpHi)
      Debug.Print "tmpLow: "; CStr(tmpLow) & "  inHighBound:" & CStr(inHighBound)
      If (inLowBound < tmpHi) Then
        Debug.Print "calling case inLowBound < tmpHi"
        Debug.Print "---------------------------------"
        quick_sort_file_array_by_modified file_array, inLowBound, tmpHi
      End If
      If (tmpLow < inHighBound) Then
        Debug.Print "calling case tmpLow < inHighBound"
        Debug.Print "---------------------------------"
        quick_sort_file_array_by_modified file_array, tmpLow, inHighBound
      End If
End Sub

Private Sub PrintArr(arr As Variant)
    Dim a As Variant
    Dim result As String
    For Each a In arr
        result = result & CStr(a) & "  "
    Next
    Debug.Print result
End Sub

最新更新