以下是引用片段: Private Sub xzPaiXu(a() As Double, sheng As Boolean) 'a为需要排序的数组,sheng为True则为升序排列,为False,则为降序排列。 Dim i As Integer, j As Integer Dim temp As Double Dim m As Integer For i = LBound(a) To UBound(a) - 1 '进行数组大小-1轮比较 m = i '在第i轮比较时,假定第 'i个元素为最值元素 For j = i + 1 To UBound(a) '在剩下的元素中找出最 '值元素的下标并记录在m中 If sheng Then '若为升序,则m记录最小元素 '下标,否则记录最大元素下标 If a(j) < a(m) Then m = j Else If a(j) > a(m) Then m = j End If Next j '将最值元素与第i个元素交换 temp = a(i) a(i) = a(m) a(m) = temp Next i End Sub 调用该过程示例: Option Base 1 Private Sub Command1_Click() Dim b(6) As Double b(1) = 8 b(2) = 6 b(3) = 9 b(4) = 3 b(5) = 2 b(6) = 7 Call xzPaiXu(b, True) For i% = 1 To 6 Print b(i) Next End Sub
以下是引用片段: Private Sub mpPaiXu(a() As Double, sheng As Boolean) 'a为需要排序的数组,sheng为True则为升序排列,为False,则为降序排列。 Dim i As Integer, j As Integer Dim temp As Double Dim m As Integer For i = LBound(a) To UBound(a) - 1 '进行n-1轮比较 For j = UBound(a) To i + 1 Step -1 '从n到i个元素两两进行比较