Sub QuickSort(List() As Double) ' Sorts an array using Quick Sort algorithm ' Adapted from "Visual Basic Developers Guide" ' By D.F. Scott Dim i As Double, j As Double, b As Double Dim l As Double, t As Double, r As Double, d As Double Dim p(1 To 100) As Double Dim w(1 To 100) As Double k = 1 p(k) = LBound(List) w(k) = UBound(List) l = 1 d = 1 r = UBound(List) Do toploop: If r - l < 9 Then GoTo bubsort i = l j = r While j > i comp = comp + 1 If List(i) > List(j) Then swic = swic + 1 t = List(j) oldx1 = List(j) oldy1 = j List(j) = List(i) oldx2 = List(i) oldy2 = i newx1 = List(j) newy1 = j List(i) = t newx2 = List(i) newy2 = i d = -d End If If d = -1 Then j = j - 1 Else i = i + 1 End If Wend j = j + 1 k = k + 1 If i - l < r - j Then p(k) = j w(k) = r r = i Else p(k) = l w(k) = i l = j End If d = -d GoTo toploop bubsort: If r - l > 0 Then For i = l To r b = i For j = b + 1 To r comp = comp + 1 If List(j) <= List(b) Then b = j Next j If i <> b Then swic = swic + 1 t = List(b) oldx1 = List(b) oldy1 = b List(b) = List(i) oldx2 = List(i) oldy2 = i newx1 = List(b) newy1 = b List(i) = t newx2 = List(i) newy2 = i End If Next i End If l = p(k) r = w(k) k = k - 1 Loop Until k = 0 End Sub Sub BubbleSort(List() As Double) ' Sorts an array using bubble sort algorithm Dim First As Double, Last As Double Dim i As Integer, j As Integer Dim Temp As Double
First = LBound(List) Last = UBound(List) For i = First To Last - 1 For j = i + 1 To Last If List(i) > List(j) Then Temp = List(j) List(j) = List(i) List(i) = Temp End If Next j Next i End Sub
Private Sub cmdBubble_Click()Dim i As Integer Dim Array1() As DoubleReDim Array1(1 To 13)For i = 1 To 13 Array1(i) = CDbl(txtS(i).Text) Next iCall BubbleSort(Array1)For i = 1 To 13 txtS(i).Text = CStr(Array1(i)) Next iEnd SubPrivate Sub cmdexit_Click() End End SubPrivate Sub cmdQuickSort_Click()Dim i As Integer Dim Array1() As DoubleReDim Array1(1 To 13)For i = 1 To 13 Array1(i) = CDbl(txtS(i).Text) Next iCall QuickSort(Array1)For i = 1 To 13 txtS(i).Text = CStr(Array1(i)) Next iEnd Sub'txtS(i)里你随便加入一些数字就可以了
Private Sub Command1_Click() Print OrderDown("0,1,7,2,4,9,5,3,8,6") End SubPrivate Function OrderDown(TempStr As String) Dim TempStr2 As String Dim Str1 As String, S1 As String Dim T1 As Long, T2 As Long Dim Loopon As Long Dim NumMax As Long, NumMin As Long Dim NumArray TempStr2 = TempStr & "," NumArray = Split(TempStr, ",") NumMax = NumArray(0) NumMin = NumArray(0)For i = 1 To UBound(NumArray) '取出最大值和最小值 If NumMax < NumArray(i) Then NumMax = NumArray(i) If NumMin > NumArray(i) Then NumMin = NumArray(i) NextFor i = NumMin To NumMax If InStr(TempStr2, i) Then T1 = InStr(TempStr2, i) T2 = InStr(T1, TempStr2, ",") + 1 S1 = Mid(TempStr2, T1, T2 - T1) Str1 = Str1 & S1 TempStr2 = Replace(TempStr2, S1, "") End If Next OrderDown = Mid(Str1, 1, Len(Str1) - 1) End Function================================================== 俺出这一段,希望大家帮我评论一下,这种子方式好不好?
Dim First As Double, Last As Double Dim i As Integer, j As Integer Dim Temp As Double First = LBound(List) Last = UBound(List) For i = First To Last - 1 For j = i + 1 To Last If List(i) > List(j) Then Temp = List(j) List(j) = List(i) List(i) = Temp End If Next j Next i vb中的冒泡排序
'冒泡排序如果序列中只有一两个数据是乱的话,那么冒泡是最快的 '但是序列中数据如果是随机排列的,那么冒泡就是最慢的了 '本例使用了选择排序法,算法简单,好维护Private Sub Form_Load() Dim list(6) As Integer list(1) = 10 list(2) = 123 list(3) = 1 list(4) = 19 list(5) = 23 list(6) = 55 sort list(), 1, 6End Sub Sub sort(list() As Integer, min As Integer, max As Integer) Dim i As Integer Dim j As Integer Dim best_value As Integer Dim value_index As IntegerFor i = min To max best_value = list(i) value_index = i For j = i + 1 To max If list(j) < best_value Then best_value = list(j) value_index = j End If Next j list(value_index) = list(i) list(i) = best_value Debug.Print list(i) Next iEnd Sub
' Sorts an array using Quick Sort algorithm
' Adapted from "Visual Basic Developers Guide"
' By D.F. Scott Dim i As Double, j As Double, b As Double
Dim l As Double, t As Double, r As Double, d As Double Dim p(1 To 100) As Double
Dim w(1 To 100) As Double k = 1
p(k) = LBound(List)
w(k) = UBound(List)
l = 1
d = 1
r = UBound(List)
Do
toploop:
If r - l < 9 Then GoTo bubsort
i = l
j = r
While j > i
comp = comp + 1
If List(i) > List(j) Then
swic = swic + 1
t = List(j)
oldx1 = List(j)
oldy1 = j
List(j) = List(i)
oldx2 = List(i)
oldy2 = i
newx1 = List(j)
newy1 = j
List(i) = t
newx2 = List(i)
newy2 = i
d = -d
End If
If d = -1 Then
j = j - 1
Else
i = i + 1
End If
Wend
j = j + 1
k = k + 1
If i - l < r - j Then
p(k) = j
w(k) = r
r = i
Else
p(k) = l
w(k) = i
l = j
End If
d = -d
GoTo toploop
bubsort:
If r - l > 0 Then
For i = l To r
b = i
For j = b + 1 To r
comp = comp + 1
If List(j) <= List(b) Then b = j
Next j
If i <> b Then
swic = swic + 1
t = List(b)
oldx1 = List(b)
oldy1 = b
List(b) = List(i)
oldx2 = List(i)
oldy2 = i
newx1 = List(b)
newy1 = b
List(i) = t
newx2 = List(i)
newy2 = i
End If
Next i
End If
l = p(k)
r = w(k)
k = k - 1
Loop Until k = 0
End Sub
Sub BubbleSort(List() As Double)
' Sorts an array using bubble sort algorithm Dim First As Double, Last As Double
Dim i As Integer, j As Integer
Dim Temp As Double
First = LBound(List)
Last = UBound(List)
For i = First To Last - 1
For j = i + 1 To Last
If List(i) > List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i
End Sub
Dim Array1() As DoubleReDim Array1(1 To 13)For i = 1 To 13
Array1(i) = CDbl(txtS(i).Text)
Next iCall BubbleSort(Array1)For i = 1 To 13
txtS(i).Text = CStr(Array1(i))
Next iEnd SubPrivate Sub cmdexit_Click()
End
End SubPrivate Sub cmdQuickSort_Click()Dim i As Integer
Dim Array1() As DoubleReDim Array1(1 To 13)For i = 1 To 13
Array1(i) = CDbl(txtS(i).Text)
Next iCall QuickSort(Array1)For i = 1 To 13
txtS(i).Text = CStr(Array1(i))
Next iEnd Sub'txtS(i)里你随便加入一些数字就可以了
Print OrderDown("0,1,7,2,4,9,5,3,8,6")
End SubPrivate Function OrderDown(TempStr As String)
Dim TempStr2 As String
Dim Str1 As String, S1 As String
Dim T1 As Long, T2 As Long
Dim Loopon As Long
Dim NumMax As Long, NumMin As Long
Dim NumArray
TempStr2 = TempStr & ","
NumArray = Split(TempStr, ",")
NumMax = NumArray(0)
NumMin = NumArray(0)For i = 1 To UBound(NumArray) '取出最大值和最小值
If NumMax < NumArray(i) Then NumMax = NumArray(i)
If NumMin > NumArray(i) Then NumMin = NumArray(i)
NextFor i = NumMin To NumMax
If InStr(TempStr2, i) Then
T1 = InStr(TempStr2, i)
T2 = InStr(T1, TempStr2, ",") + 1
S1 = Mid(TempStr2, T1, T2 - T1)
Str1 = Str1 & S1
TempStr2 = Replace(TempStr2, S1, "")
End If
Next
OrderDown = Mid(Str1, 1, Len(Str1) - 1)
End Function==================================================
俺出这一段,希望大家帮我评论一下,这种子方式好不好?
Dim i As Integer, j As Integer
Dim Temp As Double
First = LBound(List)
Last = UBound(List)
For i = First To Last - 1
For j = i + 1 To Last
If List(i) > List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i
vb中的冒泡排序
'但是序列中数据如果是随机排列的,那么冒泡就是最慢的了
'本例使用了选择排序法,算法简单,好维护Private Sub Form_Load()
Dim list(6) As Integer
list(1) = 10
list(2) = 123
list(3) = 1
list(4) = 19
list(5) = 23
list(6) = 55
sort list(), 1, 6End Sub
Sub sort(list() As Integer, min As Integer, max As Integer)
Dim i As Integer
Dim j As Integer
Dim best_value As Integer
Dim value_index As IntegerFor i = min To max
best_value = list(i)
value_index = i
For j = i + 1 To max
If list(j) < best_value Then
best_value = list(j)
value_index = j
End If
Next j
list(value_index) = list(i)
list(i) = best_value
Debug.Print list(i)
Next iEnd Sub