元素索引从最小到最大

Oli*_*ham 5 arrays vba max min excel-vba

我正在尝试为群组平衡问题制作一个初始解决方案,但我似乎陷入了一些听起来应该很简单的事情.

基本上我有一个权重数组(随机整数),例如

W() = [1, 4, 3, 2, 5, 3, 2, 1]
Run Code Online (Sandbox Code Playgroud)

我想创建另一个长度相同的数组,数字1到数组的大小,分别代替最小到最大的数字,例如

S() = [1, 7, 5, 3, 8, 6, 4, 2]
Run Code Online (Sandbox Code Playgroud)

对于重复项,第一次出现的是较小的索引.

我最初使用的是BubbleSort算法,但不幸的是,这不允许我以所需的格式提供输出.

我知道这是一个非常具体的问题,但任何帮助将不胜感激.

Oli*_*ham 0

非常感谢所有给予帮助的人!

我采纳了你的建议,并以某种方式设法制定了自己的解决方案,尽管我花了一整天的时间来处理与我的整个项目无关的事情。

这是我使用的以下代码:

Sub InitialSol(S() As Integer, n As Integer, k As Integer, W() As Long)
Dim i As Integer, c As Integer
Dim min As Long, max As Long, temp As Long

min = W(1)
max = W(1)
For i = 2 To n
    If W(i) <= min Then
        min = W(i)
    End If
    If W(i) >= max Then
        max = W(i)
    End If
Next i

c = 1
Do While c <= n
    temp = max
    For i = 1 To n
        If W(i) = min Then
            S(i) = c
            c = c + 1
        End If
    Next i
    For i = 1 To n
        If W(i) > min And W(i) <= temp Then
            temp = W(i)
        End If
    Next i
    min = temp
Loop

End Sub
Run Code Online (Sandbox Code Playgroud)