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排序算法比较
快速排序算法是目前最好的,尤其是系统函数的效率相当高。