1 回答
TA贡献1777条经验 获得超10个赞
'在模块中粘贴下面的代码,“工程属性-启动对象”要改为“Sub Main”
Sub Swap(a() As Long, i As Long, j As Long)
Dim t As Long
t = a(i)
a(i) = a(j)
a(j) = t
End Sub
Sub SelSort(a() As Long, n As Long, incr As Long)
Dim i As Long, j As Long
For i = incr To (n - 1)
For j = i To incr Step -1
If j >= incr And a(j) < a(j - incr) Then Swap a, j, j - incr
Next j
Next i
End Sub
Sub ShellSort(a() As Long, n As Long)
Dim i As Long, j As Long
For i = n / 2 To 3 Step -2
For j = 0 To (i - 1)
SelSort a, n - j, i
SelSort a, n, 1
Next j
Next i
End Sub
Sub Main()
Dim i As Long
Dim a() As Long
Do
ReDim Preserve a(i)
a(i) = Val(InputBox("请输入第 " & (i + 1) & " 个数,输入小于0的数结束"))
i = i + 1
Loop Until a(i - 1) < 0
i = UBound(a)
If i > 0 Then
ReDim Preserve a(i - 1)
ShellSort a, UBound(a) + 1
For i = 0 To UBound(a)
Debug.Print a(i)
Next i
End If
End Sub
添加回答
举报