vb冒泡排序代码
发布网友
发布时间:2022-06-06 02:00
我来回答
共4个回答
热心网友
时间:2022-05-22 03:03
随即产生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爱好者,以后互相学习,互相努力哦!!!^_^
热心网友
时间:2022-05-22 04:21
冒泡排序法:
Option Explicit
Private Sub Form_Click()
Const N = 10
Dim i%, a(1 To N), m%, j%, t%
Randomize
For i = 1 To N '随机输出10个1位数
a(i) = Int(Rnd * 9 + 1)
Print a(i);
Next i
Print
For i = 1 To N - 1 '采用冒泡排序
For j = 1 To N - 1
If a(j) > a(j + 1) Then
t = a(j): a(j) = a(j + 1): a(j + 1) = t
End If
Next j
Next i
For i = 1 To N '输出排序后的数字
Print a(i);
Next i
Print
热心网友
时间:2022-05-22 05:55
Private Sub Command1_Click()
Dim a(5) As Double
Dim i, j As Integer
'给数组赋值
For i = 1 To 5
temp = InputBox(" 请输入" + Str(i) + "个数字", "输入")
If IsNumeric(temp) Then
'是数字就将数字赋值给数组
a(i) = temp
Else
'不是数字就将数组置0
a(i) = 0
End If
Next i
'排序
For i = 1 To 4
For j = 1 To 5 - i
If a(j) < a(j + 1) Then
temp = a(j)
a(j) = a(j + 1)
a(j + 1) = temp
End If
Next j
Next i
MsgBox Str(a(1)) + ", " + Str(a(2)) + ", " + Str(a(3)) + ", " + Str(a(4)) + ", " + Str(a(5))
End Sub
热心网友
时间:2022-05-22 07:47
Dim i, j, intmin As Integer, n As Integer
Dim sngsq(), sngt As Single '输入原始数据
n = Val(InputBox("请输入排序的个数N的值"))
ReDim sngsq(n) '重新定义数组
Print "排序前"
For i = 1 To n
MsgBox "请输入第" & Str(i) & "个数:"
sngsq(i) = Val(InputBox("请输入")) '将输入的数据转换为数值型
Print sngsq(i);
Next
Print
Print "排序后"
'排序
For i = 1 To n - 1
intmin = i '对第i遍排序,初始假定第i个元素最小
For j = i + 1 To n '找出最小的元素序号
If sngsq(j) < sngsq(intmin) Then intmin = j
Next j
'将找出的最小元素与第i个元素交换
sngt = sngsq(i)
sngsq(i) = sngsq(intmin)
sngsq(intmin) = sngt
Next i
'打印输出
For i = 1 To n
Print sngsq(i); '不换行输出
Next i