用冒泡排序法
Private Sub Command1_Click()
Dim a(1 To 100) As Integer, t As Integer
Dim i As Integer, j As Integer
Dim Min As Integer
Randomize
For i = 1 To 100
a(i) = Rnd() 1000
Next
For i = 1 To 100
Text1Text = Text1Text & "," & a(i)
Next
'起泡排序
' For i = 1 To 99
' For j = 1 To 100 - i
' If a(j) > a(j + 1) Then
' t = a(j): a(j) = a(j + 1): a(j + 1) = t
' End If
' Next
' Next
'选择排序
For i = 1 To 99
Min = i
For j = Min + 1 To 100
If a(Min) > a(j) Then
Min = j
End If
Next
t = a(i): a(i) = a(Min): a(Min) = t
Next
For i = 1 To 100
Text2Text = Text2Text & "," & a(i)
Next i
End Sub
我可以帮助你,你先设置我最佳答案后,我百度Hii教你。
实例:
Private Sub Form_Click()
Dim a(4) As Double
Dim n As Double
Dim i, j, k, t
For i = 0 To 4
a(i) = InputBox("请输入:" , "选择法排序")
Next i
For i = 0 To 3
k = i
For j = i + 1 To 4
If a(k) > a(j) Then k = j
Next j
If k <> i Then t = a(k): a(k) = a(i): a(i) = t
Next i
For i = 0 To 4
Print a(i)
Next i
End Sub
请参看我的回答,有2个~~
这个问题很老了~~~,
1输出最大和最小数字
text1 MultiLine 属性ture,在text1里面,输入一个数字后回车,再输入下一个
==============================================
Private Sub Command1_Click()
Dim NM() As String, Max As Integer, Min As Integer, S As String
Dim I As Integer
S = Replace(Replace(Replace(Text1Text, vbCrLf, ","), " ", ""), ",,", ",")
If Right$(S, 1) = "," Then S = Left$(S, Len(S) - 1)
NM = Split(S, ",")
Max = NM(0)
Min = NM(0)
For I = 1 To UBound(NM)
If Max < NM(I) Then
Max = NM(I)
End If
If Min > NM(I) Then
Min = NM(I)
End If
Next
Print "最大数是:" & Max & vbCrLf & "最小数是:" & Min
End Sub
======================================
2所有数字排序
窗体中放一个text控件,然后在里面输入数字,按回车,每输一个数,按一次回车
Dim i As Integer
Dim arr(10) As Long
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 13 Then Exit Sub
i = i + 1
MeCaption = "输入第 " + CStr(i + 1) + " 个数"
If i < 10 Then
arr(i + 1) = Int(Text1Text)
Else
i = 0
For x = 1 To 10
For y = 2 To x
If (arr(x) < arr(y)) Then
arr(0) = arr(x)
arr(x) = arr(y)
arr(y) = arr(0)
End If
Next
Next
MeCaption = "输入第 " + CStr(i + 1) + " 个数"
Print "大到小:"
For m = 1 To 10
Print arr(m)
Next
End If
End Sub
其实从思想上来说,就是一个排序的思路问题。
排序的方法有很多。
说2个最简单的(设让结果从小到大排列)
方法一
假设10个数
把第一个依次与他后面的比较,如果比后面的大,就把位子换下,这样当第一个看完后面9个数后第一个数就变成最小的数了
再把第二个依次与后面8个比较,让第二个变成9个中最小的
以此类推,每次都拿还没比出顺序的第一个数和它后面的每个数比较
这样当第九个数拿出来和第十个数比完后,整个数列就变成从小到大排列的了
方法二(叫做冒泡)
依次拿第一个数和第二个比,第二个和第三个比,凡是前面的数比后面的数大的话就换位子,当第九个和第十个比完后,第十个位子永远是最大的数。然后再对前9个数再做一次这个事情,那第九个数就变9个里最大的了,直到最后一次第一个数和第二个数比完(此时后面8个数已经按序排列了)后,整个数列就变成了从小到大排列了
Private Sub Command1_Click()
Dim a(10) As Integer
Dim b As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
For i = 1 To 10
a(i) = Int(Rnd 90 + 10)
Print a(i);
Next i
For i = 1 To 9
k = i
For j = k + 1 To 10
If a(j) < a(k) Then k = j
Next j
If a(i) > a(k) Then
b = a(i)
a(i) = a(k)
a(k) = b
End If
Next i
For i = 1 To 10
Print a(i);
Next i
End Sub
稍微修改了一下:
Private Sub Command2_Click()
List2Clear
n = Val(Text1Text)
For i = 1 To n - 1
min = i
For j = i + 1 To n
If d(min) > d(j) Then
min = j
End If
Next j
If min <> i Then
k = d(i)
d(i) = d(min)
d(min) = k
End If
Next i
For i = 1 To n
List2AddItem Str(d(i))
Next i
End Sub
If e(i) < e(j) Then k = j
这后面是 k=j ,跟i没有任何关系,所以应该是
If e(k) < e(j) Then k = j
由于then后面的语句与then是写在同一行的,所以不用End If。如果是下面这样就需要End If:
If e(k) < e(j) Then
k = j
End If
另外,你这个k纯属多余,可以改为:
Private Sub Command1_Click()Dim e(20) As Integer
For i = 1 To 5
e(i) = Val(InputBox("请输入第" & i & "个成绩:", "输入"))
Next i
For i = 1 To 4
For j = i + 1 To 5
If e(i) < e(j) Then
t = e(i)
e(i) = e(j)
e(j) = t
End If
Next j
Print e(i)
Next i
End Sub
以上就是关于编写vb程选择法,冒泡法全部的内容,包括:编写vb程选择法,冒泡法、VB 的基本选择排序算法。谁能讲一下。基础一点的、VB中任意输入十个数运用选择排序法运行等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)