编写代码定义一个6个元素的整形数组并赋值这个数组,然后用冒泡排序下,打印每次结果。
小弟没有点思路啊,大家帮帮忙
'//////////////////////
'
'冒泡排序求最大、最小值
'作者:griefforyou
'
'/////////////////////
'实际上我们只要的是最大值和最小值,可以不使用冒泡算法,用2楼的方法即可
'但是冒泡算法确实也是可以完成你要的功能的。
Option Explicit
Private Sub Command1_Click()
MsgBox "最大值为:" & Max(6, 9, 3, 14, 7, 1)
MsgBox "最小值为:" & Min(6, -1, 3, 4, 0, 7, 1)
End Sub
'求不定个数数值中最大值
'调用方法:Max(1,3,[n]...)
Private Function Max(ParamArray Number() As Variant) As Integer
Dim SortedArray() As Variant
SortedArray = Number
SortedArray = ArraySort(SortedArray, True)
Max = SortedArray(0)
End Function
'求不定个数数值中最小值
'调用方法:Min(1,3,[n]...)
Private Function Min(ParamArray Number() As Variant) As Integer
Dim SortedArray() As Variant
SortedArray = Number
SortedArray = ArraySort(SortedArray, False)
Min = SortedArray(0)
End Function
'数组冒泡排序算法
'Flag为False为升序,True为降序
'返回值为排序完的数组
Private Function ArraySort(NumberArr As Variant, Flag As Boolean)
Dim i As Integer, j As Integer
Dim Temp As Variant
For i = 0 To UBound(NumberArr)
For j = 0 To UBound(NumberArr) - 1
If Flag = False Then
If NumberArr(j) > NumberArr(j + 1) Then
Temp = NumberArr(j)
NumberArr(j) = NumberArr(j + 1)
NumberArr(j + 1) = Temp
End If
Else
If NumberArr(j) < NumberArr(j + 1) Then
Temp = NumberArr(j)
NumberArr(j) = NumberArr(j + 1)
NumberArr(j + 1) = Temp
End If
End If
Next
Next
ArraySort = NumberArr
End Function