gpt4 book ai didi

arrays - VBA数组排序功能?

转载 作者:行者123 更新时间:2023-12-03 05:15:25 25 4
gpt4 key购买 nike

我正在寻找 VBA 中数组的合适排序实现。快速排序将是首选。或任何其他sort algorithm除了冒泡或合并就足够了。

请注意,这是为了与 MS Project 2003 配合使用,因此应避免任何 Excel native 函数和任何 .net 相关的内容。

最佳答案

看看here :
编辑:引用的来源 (allexperts.com) 已关闭,但以下是相关的 author评论:

There are many algorithms available on the web for sorting. The most versatile and usually the quickest is the Quicksort algorithm. Below is a function for it.

Call it simply by passing an array of values (string or numeric; it doesn't matter) with the Lower Array Boundary (usually 0) and the Upper Array Boundary (i.e. UBound(myArray).)

Example: Call QuickSort(myArray, 0, UBound(myArray))

When it's done, myArray will be sorted and you can do what you want with it.
(Source: archive.org)

Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)
Dim pivot As Variant
Dim tmpSwap As Variant
Dim tmpLow As Long
Dim tmpHi As Long

tmpLow = inLow
tmpHi = inHi

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

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

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

If (tmpLow <= tmpHi) Then
tmpSwap = vArray(tmpLow)
vArray(tmpLow) = vArray(tmpHi)
vArray(tmpHi) = 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

请注意,这仅适用于单维(又名“正常”?)数组。 (有一个有效的多维数组 QuickSort here 。)

关于arrays - VBA数组排序功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/152319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com