扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
如果五个号码为数组元素(1)到(5),正确的排序过程:
创新互联公司专注于网站建设,为客户提供网站设计制作、网站设计、网页设计开发服务,多年建网站服务经验,各类网站都可以开发,品牌网站建设,公司官网,公司展示网站,网站设计,建网站费用,建网站多少钱,价格优惠,收费合理。
对于i = 1至4
对于L = 1到5 - 如果A(L)(L +1)
N = A(L)
A(L),= A(L +1)
(L +1) =
结束如果下一页l
接下来,我
能够到第一台计算机来验证结果,然后分析程序。
看上面那一大段,我晕了(╯﹏╰)b
下面的是从小到大排序的,从大到小只需改一个“”号,看下面的注释,整个过程不变的。
Private Sub Command1_Click()
Dim x(1 To 5) As Double, i%, j%, t
x(1) = Val(Text1)
x(2) = Val(Text2)
x(3) = Val(Text3)
x(4) = Val(Text4)
x(5) = Val(Text5)
For i = 1 To 5 - 1 '有5个数要排序,需要进行5-1=4次两两比较
For j = 5 To i + 1 Step -1 '从末尾一个元素到第i个元素进行两两比较
If x(j) x(j - 1) Then '从小到大排序,若需要从大到小排序,将“”改为“”
'若次序不对,则马上交换位置
t = x(j)
x(j) = x(j - 1)
x(j - 1) = t
End If
Next j '除了内循环,一轮排序结束,最小(或最大)数已经冒到数组第一位
Next i
Label1.Caption = x(1)
Label2.Caption = x(2)
Label3.Caption = x(3)
Label4.Caption = x(4)
Label5.Caption = x(5)
End Sub
给你一个最简单的冒泡排序代码:
将三个数放到一个数组中。
a(0)=val(text1.text):a(1)=val(text2.text):a(2)=val(text3.text)
dim flag as Boolean,temp as Integer
for i = 0 to 2
flag = true
for j = 2 to 1
if a(j)a(j-1) then
temp = a(j-1)
a(j-1) = a(j)
a(j) = temp
flag = false
end if
next j
if flag then Exit For
next i
text4.text=a(0):text5.text=a(1):text6.text=a(2)
随即产生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爱好者,以后互相学习,互相努力哦!!!^_^
Private Sub Command1_Click()
Dim a(1 To 100) As Integer
Dim i As Integer, j As Integer, k As Integer
For i = 1 To 100 '给数组a一百个元素赋值,并换每行十个数字输出来窗体上
a(i) = Int(Rnd * 101)
k = k + 1
Print Tab((k - 1) * 5); a(i);
If k = 10 Then k = 0: Print
Next i
For i = 100 To 2 Step -1 '用冒泡排序法对数组进行排序
For j = 1 To i - 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 100 '输出排好序的数组
k = k + 1
Print Tab((k - 1) * 5); a(i);
If k = 10 Then k = 0: Print
Next i
End Sub
试试看:
For i = LBound(moto) To UBound(moto) - 1
For j = LBound(moto) To UBound(moto) - 1 - i
If moto(j) moto(j + 1) Then
t = moto(j)
moto(j) = moto(j + 1)
moto(j + 1) = t
End If
Next j
Next i
For i = LBound(moto) To UBound(moto)
Print moto(i);
Next i
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流