vb排序演算法
① VB編程排序法
待解決
Private Sub Command1_Click()
dtys Label1(0), temp
dtys Label1(1), Label1(0)
dtys temp, Label1(1)
End Sub
Sub dtys(a As Object, b As Object)
Dim x%, y%, i%, j%, k%
Const DelayN = 1000
x = a.Left: y = a.Top
For i = 1 To DelayN
j = x + (b.Left - x) / DelayN * i
k = y + (b.Top - y) / DelayN * i
a.Move j, k
a.Move x, y
Next i
b = a
DoEvents
End Sub
② VB編程怎麼排序
冒泡排序法思路:(假設有10個數,需要從小到大排)
Dim a(10)
Randomize
For i = 1 To 10
a(i) = Int(Rnd * i + 3 + Rnd * 10) '給數組賦值
Print a(i);
Next i
Print
For i = 1 To 10 『
For j = i + 1 To 10
If a(j) < a(i) Then 』如果剩餘的比a(i)小的話,就交換
t = a(j)
a(j) = a(i)
a(i) = t
End If
Next j
Next i
For k = 1 To 10
Print a(k); 『列印排好的數
Next k
Print
、
冒泡排序(Bubble Sort),是一種計算機科學領域的較簡單的排序演算法。
它重復地走訪過要排序的數列,一次比較兩個元素,如果他們的順序錯誤就把他們交換過來。走訪數列的工作是重復地進行直到沒有再需要交換,也就是說該數列已經排序完成。
這個演算法的名字由來是因為越大的元素會經由交換慢慢「浮」到數列的頂端,故名。
③ VB排序演算法
2,3,5,15,68,55
13,5,6,87,2,8,98
55,145,4,621,54,31,
2,5,54,6345,66
8,123,54,64,12,5
4,5,46,5,12,5,5
6,1,2,54,1
1,2,45,41,23
5,1,56,31,45
我把TXT中的內容寫成這樣,應該是合乎你要求的吧?然後代碼如下:
Option Explicit
Dim Arry() As String
Private Sub Command1_Click()
Dim i As Long, j As Long
Dim x As String, y As String
i = 1
ReDim Preserve Arry(i) As String
Open App.Path & "\111.txt" For Input As #1
While Not EOF(1)
Line Input #1, Arry(i)
i = i + 1
ReDim Preserve Arry(i) As String
Wend
ReDim Preserve Arry(i - 1) As String
Close #1
For i = 1 To UBound(Arry)
For j = 1 To UBound(Arry)
If j < UBound(Arry) Then
If Val(Arry(j)) > Val(Arry(j + 1)) Then
x = Arry(j)
y = Arry(j + 1)
Arry(j) = y
Arry(j + 1) = x
End If
End If
Next
Next
Open App.Path & "\222.txt" For Output As #1
For i = 1 To UBound(Arry)
Print #1, Arry(i)
Next i
Close #1
End Sub
經過上機測試,生成的222.TXT中的內容如下:
1,2,45,41,23
2,3,5,15,68,55
2,5,54,6345,66
4,5,46,5,12,5,5
5,1,56,31,45
6,1,2,54,1
8,123,54,64,12,5
13,5,6,87,2,8,98
55,145,4,621,54,31,
首位是排好序的。
④ VB插入排序演算法
Option Explicit
Private Sub Form_Load()
Dim i As Integer, intC As Integer
Dim IntOuSus() As String, strOuSus As String
Dim IntJiSus() As String, strJiSus As String
Randomize
For i = 1 To 30
intC = CInt(Rnd * 90) + 10
If intC Mod 2 = 1 Then
strJiSus = strJiSus & IIf(strJiSus = "", "", ",") & intC
Else
strOuSus = strOuSus & IIf(strOuSus = "", "", ",") & intC
End If
Next
IntOuSus = Split(strOuSus, ",")
IntJiSus = Split(strJiSus, ",")
OrderNumbers IntJiSus, "ASC"
OrderNumbers IntOuSus, "DESC"
End Sub
'注意以下過程中第一個參數使用了ByRef
'此方法比傳統的冒泡法快得多
Private Sub OrderNumbers(ByRef a() As String, Optional ByVal ASCDESC As String = "ASC")
Dim min As Long, max As Long, num As Long, first As Long, last As Long, temp As Long, all As New Collection, steps As Long
ASCDESC = UCase(ASCDESC)
min = LBound(a)
max = UBound(a)
all.Add a(min)
steps = 1
For num = min + 1 To max
last = all.Count
If a(num) < CDbl(all(1)) Then all.Add a(num), BEFORE:=1: GoTo nextnum '加到第一項
If a(num) > CDbl(all(last)) Then all.Add a(num), AFTER:=last: GoTo nextnum '加到最後一項
first = 1
Do While last > first + 1 '利用DO循環減少循環次數
temp = (last + first) \ 2
If a(num) > CDbl(all(temp)) Then
first = temp
Else
last = temp
steps = steps + 1
End If
Loop
all.Add a(num), BEFORE:=last '加到指定的索引
nextnum:
steps = steps + 1
Next
For num = min To max
If ASCDESC = "ASC" Then a(num) = CDbl(all(num - min + 1)): steps = steps + 1 '升序
If ASCDESC = "DESC" Then a(num) = CDbl(all(max - num + 1)): steps = steps + 1 '降序
Next
'MsgBox "本數組共經過 " & steps & "步實現" & IIf(ASCDESC = "ASC", "升序", "降序") & "排序!", 64, "INFORMATION"
Set all = Nothing
End Sub
⑤ vb冒泡排序代碼
隨即產生10個0~100的數,並按從小到大排序,已經驗證OK!!!
Private Sub Command1_Click()
Dim num(1 To 10) As Integer
Dim i, j, t As Integer
For i = 1 To 10
num(i) = Int((100 - 0 + 1) * Rnd + 0)
Next
For i = 1 To 10
For j = i + 1 To 10
If num(i) > num(j) Then
t = num(i)
num(i) = num(j)
num(j) = t
End If
Next j
Next i
For i = 1 To 10
Print num(i)
Next
End Sub
我也是VB愛好者,以後互相學習,互相努力哦!!!^_^
⑥ VB冒泡排序演算法
隨即產生10個0~100的數,並按從小到大排序
Private Sub Command1_Click()
Dim num(1 To 10) As Integer
Dim i, j, t As Integer
For i = 1 To 10
num(i) = Int((100 - 0 + 1) * Rnd + 0)
Next
For i = 1 To 10
For j = i + 1 To 10
If num(i) > num(j) Then
t = num(i)
num(i) = num(j)
num(j) = t
End If
Next j
Next i
For i = 1 To 10
Print num(i)
Next
End Sub
⑦ VB 關於選擇排序演算法
'在窗體上增加控制項command1,label1,然後復制以下代碼
Option Explicit
Dim a(9) As Long
Private Sub Command1_Click()
Dim i As Long, l As Long, n As Long
Dim j As Long, s As String
For i = 0 To 9 '使用選擇排序法排序,每次選擇最小的數值。
For l = i To 9
If a(i) > a(l) Then
n = a(i)
a(i) = a(l)
a(l) = n
End If
Next l
Print a(i)
s = ""
For j = 0 To 9
s = s & a(j) & vbNewLine
Next
MsgBox s, vbInformation, "階段排序情況"
Next i
End Sub
Private Sub Form_Load()
a(0) = 564 '給數組a中個數組元素賦值。
a(1) = 78
a(2) = 45
a(3) = 456412
a(4) = 456
a(5) = 1
a(6) = 45 + 79
a(7) = 12
a(8) = 1 * 966
a(9) = 65 / 5
Dim i As Long
For i = 0 To 9
Label1.Caption = Label1.Caption & "第" & CStr(i + 1) & "是:" & CStr(a(i)) & " "
Next i
End Sub
⑧ VB選擇法排序
PrivateSub Command1_Click()
Dim a(1 To 10) As Integer
For i = 1 To 10
a(i) = Int(Rnd * 10)
Print a(i);
Next i
'以上十隨機得出10個數
For i = 1 To 9 '這里開始是雙重循環
For j = i + 1 To 10
If a(i) > a(j) Then '如果前一個數大於後一個數則交換位置
t = a(i): a(i) = a(j): a(j) = t
End If
Next j
Print a(i);
Next i
Print a(i);End Sub
I等於1時 就是拿第一位和第二位比較 比較大的的數排後面 然後比較第一位和第三位 For j = i + 1
To 10 直到比較到最後一位
i=2 時也是如此 循環9次就完成排序5 3 2 7
i=1 時
J=2時 3 5 2 7
J=3時 2 5 3 7
j=4時 2 5 3 7
I=2時
j=3時 2 3 5 7(8)vb排序演算法擴展閱讀
PrivateSubCommand1_Click()
Dima(10)AsInteger
DimbAsInteger
DimiAsInteger
DimjAsInteger
DimkAsInteger
Fori=1To10
a(i)=Int(Rnd*90+10)
Printa(i);
Nexti
PrintFori=1To9
k=i
Forj=k+1To10
Ifa(j)<a(k)Thenk=j
Nextj
Ifa(i)>a(k)Then
b=a(i)
a(i)=a(k)
a(k)=bEndIf
NextiFori=1To10
Printa(i);Nexti
⑨ vb 快速排序
最內層的兩個DO循環中,你用的是While,這時有個問題,就是當while成立時,該DO循環還會再執行一次循環體,你可以改成Until試試。
⑩ vb排序演算法比較
快速排序演算法是目前最好的,尤其是系統函數的效率相當高。