VB冒泡法和比较法排序
1、要求是输入一个整数N,产生N个随机数(范围1—10*N),然后采用冒泡法和比较法实现由小到大的排序。将排序前后的数据显示出来。2、我已经实现了随机数的编程:Priva...
1、要求是输入一个整数N,产生N个随机数(范围1—10*N),然后采用冒泡法和比较法实现由小到大的排序。将排序前后的数据显示出来。
2、我已经实现了随机数的编程:
Private Sub Command1_Click()
Dim i, n As Integer
Print " " & "随机数:";
n = Val(Text1.Text)
For i = 1 To n
Print Int(Rnd * n);
Next i
Print
End Sub
3、我的问题是:求高手帮我编一下冒泡和排序的代码。求代码。
不要太高级、用一些高级函数之类的,简约精炼、通俗易懂者优先采纳!
真心谢谢大家!! 展开
2、我已经实现了随机数的编程:
Private Sub Command1_Click()
Dim i, n As Integer
Print " " & "随机数:";
n = Val(Text1.Text)
For i = 1 To n
Print Int(Rnd * n);
Next i
End Sub
3、我的问题是:求高手帮我编一下冒泡和排序的代码。求代码。
不要太高级、用一些高级函数之类的,简约精炼、通俗易懂者优先采纳!
真心谢谢大家!! 展开
1个回答
2012-06-09
展开全部
选择法排序:
Option Base 1
Private Sub Command1_Click()
Dim a() As Integer
Dim max As Integer
n = Val(InputBox(""))
ReDim a(10 * n)
Randomize
For i = 1 To 10 * n
a(i) = Int(Rnd * 100)
Print a(i);
Next
Print
For i = 1 To 10 * n - 1
k = i
For j = i + 1 To 10 * n
If a(k) > a(j) Then
k = j
End If
Next
t = a(k): a(k) = a(i): a(i) = t
Next
For i = 1 To 10 * n
Print a(i);
Next
Print
冒泡法排序:
Option Base 1
Private Sub Command1_Click()
Dim a() As Integer
Dim max As Integer
n = Val(InputBox(""))
ReDim a(10 * n)
Randomize
For i = 1 To 10 * n
a(i) = Int(Rnd * 100)
Print a(i);
Next
Print
For i = 1 To 10 * n - 1
For j = 10 * n To i + 1 Step -1
If a(j - 1) > a(j) Then
t = a(j): a(j) = a(j - 1): a(j - 1) = t
End If
Next
Next
For i = 1 To 10 * n
Print a(i);
Next
Print
Option Base 1
Private Sub Command1_Click()
Dim a() As Integer
Dim max As Integer
n = Val(InputBox(""))
ReDim a(10 * n)
Randomize
For i = 1 To 10 * n
a(i) = Int(Rnd * 100)
Print a(i);
Next
For i = 1 To 10 * n - 1
k = i
For j = i + 1 To 10 * n
If a(k) > a(j) Then
k = j
End If
Next
t = a(k): a(k) = a(i): a(i) = t
Next
For i = 1 To 10 * n
Print a(i);
Next
冒泡法排序:
Option Base 1
Private Sub Command1_Click()
Dim a() As Integer
Dim max As Integer
n = Val(InputBox(""))
ReDim a(10 * n)
Randomize
For i = 1 To 10 * n
a(i) = Int(Rnd * 100)
Print a(i);
Next
For i = 1 To 10 * n - 1
For j = 10 * n To i + 1 Step -1
If a(j - 1) > a(j) Then
t = a(j): a(j) = a(j - 1): a(j - 1) = t
End If
Next
Next
For i = 1 To 10 * n
Print a(i);
Next
追问
谢谢你的回答。不过,我运行了一下,只是在窗体上显示了两行很长的数字,不知道什么意思。。没有“随机数”“排序一”和“排序二”的分别显示。。麻烦你再帮帮忙看看吧,谢谢你了!!
追答
关于你说的那个问题,你可以在你的窗体上加上一下控件,和改动一下代码就行了,
”随机数“显示在窗体上的第一行上,第二行是将第一行(随机数)的数按顺序排列!运行一下就知道啦!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询