第1个回答 2010-12-02
自己搜搜排序法吧
Option Explicit
Option Base 1
Dim xx
Private Sub Command1_Click()
'选择排序法
Dim i As Integer, j As Integer, t As Integer
Debug.Print: Debug.Print "选择排序法 从小到大排列"
For i = 1 To 9
For j = i + 1 To 10
'"<"从大到小排列,xx(1)最大,">"从小到大排列,xx(1)最小
If xx(i) > xx(j) Then t = xx(i): xx(i) = xx(j): xx(j) = t
Next j
Next i
For i = 1 To 10
Debug.Print xx(i); Space(1);
Next i
End Sub
Private Sub Command2_Click()
'冒泡排序法
Dim i As Integer, j As Integer, t As Integer
Debug.Print: Debug.Print "冒泡排序法从大到小排列"
For i = 1 To 9
For j = 1 To 10 - i
'"<"从大到小排列,xx(1)最大,">"从小到大排列,xx(1)最小
If xx(j) < xx(j + 1) Then t = xx(j): xx(j) = xx(j + 1): xx(j + 1) = t
Next j
Next i
For i = 1 To 10
Debug.Print xx(i); Space(1);
Next i
End Sub
Private Sub Command3_Click()
'交换排序法
Dim i As Integer, j As Integer, p As Integer, t As Integer
Debug.Print: Debug.Print "交换排序法从小到大排列"
For i = 1 To 9
p = i
For j = i + 1 To 10
'"<"从大到小排列,xx(1)最大,">"从小到大排列,xx(1)最小
If xx(p) > xx(j) Then p = j
Next j
If p <> i Then t = xx(p): xx(p) = xx(i): xx(i) = t
Next i
For i = 1 To 10
Debug.Print xx(i); Space(1);
Next i
End Sub
Private Sub Command4_Click()
'找最大值
Dim i As Integer
For i = 2 To 10
If xx(1) < xx(i) Then xx(1) = xx(i)
Next i
Debug.Print: Debug.Print "最大值是:"; xx(1)
Form_Load
End Sub
Private Sub Command5_Click()
'找最小值
Dim i As Integer
For i = 2 To 10
If xx(1) > xx(i) Then xx(1) = xx(i)
Next i
Debug.Print: Debug.Print "最小值是:"; xx(1)
Form_Load
End Sub
Private Sub Form_Load()
xx = Array(10, -10, 20, 60, -50, 40, -12, 11, 36, 33)
End Sub