vba - VBA 中链表的快速排序

标签 vba linked-list quicksort

不幸的是,我继承了一些在 VBA 中使用 LinkedLists 的 VBA 代码,但没有任何内容被排序,并且需要排序。

链表示例: http://support.microsoft.com/kb/166394

我试图通过将以下代码转换为 LinkedList 对项目进行快速排序: VBA array sort function?

但是我很难遵循函数的逻辑来确定如何将其转换为非编号系统(例如链接列表)。

有人可以帮助注释代码以解释发生的情况,或者可能帮助翻译吗?

最佳答案

首先,您需要一个链接列表对象。我将使用一个数组作为示例。为了简单起见,我们采用 5 个节点。

'Declaration of the array
Dim LinkedList(0 To 4) As Node

现在,是时候填充数组了。我们说变量 head 是我们的 LinkedList 的头部:

Dim i As Integer
i = 0

Dim currentNode As Node
Set currentNode = head.pnext

While Not currentNode.pnext Is currentNode 'walk rest of list to end
    LinkedList(i) = currentNode
    i = i + 1
    Set currentNode = currentNode.pnext      'current pointer to next node
Wend

我们的LinkedList现已填充,我们可以使用快速排序。我们用这一行启动初始调用:

QuickSort LinkedList, LBound(LinkedList), UBound(LinkedList)

我们对这个功能做了一些调整:

Public Sub QuickSort(vArray As Node, inLow As Long, inHi As Long)

  Dim pivot   As Integer
  Dim tmpSwap As Integer
  Dim tmpLow  As Long
  Dim tmpHi   As Long

  tmpLow = inLow
  tmpHi = inHi

  pivot = vArray((inLow + inHi) \ 2).Key

  While (tmpLow <= tmpHi)

     While (vArray(tmpLow).Key < pivot And tmpLow < inHi)
        tmpLow = tmpLow + 1
     Wend

     While (pivot < vArray(tmpHi).Key And tmpHi > inLow)
        tmpHi = tmpHi - 1
     Wend

     If (tmpLow <= tmpHi) Then
        tmpSwap = vArray(tmpLow).Key
        vArray(tmpLow).Key = vArray(tmpHi).Key
        vArray(tmpHi).Key = tmpSwap
        tmpLow = tmpLow + 1
        tmpHi = tmpHi - 1
     End If

  Wend

  If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
  If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi

End Sub

我觉得还不错。如果有问题或误解请告诉我。

关于vba - VBA 中链表的快速排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16549962/

相关文章:

c - 从队列中删除偶数

c中的编译器错误链表

c - 用C语言编写的快速排序

performance - 我们什么时候应该使用基数排序?

java - 对对象数组进行快速排序

Excel VBA如何链接一个类和一个控件?

vba - Excel VBA 发送带有多个附件的电子邮件

php - 从 HTML 中移除样式

ms-access - MS Access 2010 中子表单的相对大小

c - 此 "good taste"和 "bad taste"代码中是否缺少 free()?