1 回答
TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
'在模塊中粘貼下面的代碼,“工程屬性-啟動(dòng)對(duì)象”要改為“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("請(qǐng)輸入第 " & (i + 1) & " 個(gè)數(shù),輸入小于0的數(shù)結(jié)束"))
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
添加回答
舉報(bào)
