vb 排序问题? 选择排序vb代码
vb的排序问题
Option Explicit Private Sub Command1_Click() Dim a(10) As Integer, g%, t% Static i% For i = 1 To 10 a(i) = Val(InputBox("请输入第" & i & "个数")) If a(i) = Empty Then Exit Sub g = 0 g = g + a(i) Next i Print "你输入的数组是: " For i = 1 To 10 Print a(i); Next i Print "倒换后的数组: " For i = 1 To 5 t = a(i): a(i) = a(10 - i + 1): a(10 - i + 1) = t Next i For i = 1 To 10 Print a(i); Next i End Sub |
vb的排序问题
1、算法说明
1) 选择法排序
(1) 从n个数中选出最小数的下标,出了循环,将最小数与第一个数交换位置;
(2) 除第一个数外,在剩下的n-1个数中再按方法(1)选出次小的数,与第二个数交换位置;
(3) 以此类推,最后构成递增序列。
譬如:
8 6 9 3 2 7
第一轮交换后 2 6 9 3 8 7
第二轮交换后 2 3 9 6 8 7
第三轮交换后 2 3 6 9 8 7
第四轮交换后 2 3 6 7 8 9
第五轮无交换 2 3 6 7 8 9
程序代码如下:
以下是引用片段:
Private Sub xzPaiXu(a() As Double, sheng As Boolean)
'a为需要排序的数组,sheng为True则为升序排列,为False,则为降序排列。
Dim i As Integer, j As Integer
Dim temp As Double
Dim m As Integer
For i = LBound(a) To UBound(a) - 1 '进行数组大小-1轮比较
m = i '在第i轮比较时,假定第
'i个元素为最值元素
For j = i + 1 To UBound(a) '在剩下的元素中找出最
'值元素的下标并记录在m中
If sheng Then '若为升序,则m记录最小元素
'下标,否则记录最大元素下标
If a(j) < a(m) Then m = j
Else
If a(j) > a(m) Then m = j
End If
Next j '将最值元素与第i个元素交换
temp = a(i)
a(i) = a(m)
a(m) = temp
Next i
End Sub
调用该过程示例:
Option Base 1
Private Sub Command1_Click()
Dim b(6) As Double
b(1) = 8
b(2) = 6
b(3) = 9
b(4) = 3
b(5) = 2
b(6) = 7
Call xzPaiXu(b, True)
For i% = 1 To 6
Print b(i)
Next
End Sub
2) 冒泡法排序
选择排序法在每一轮排序时找最值元素的下标,出了内循环(一轮排序结束),再交换最小数的位置;而冒泡法在每一轮排序时将相邻的数比较,当次序不对就交换位置,出了内循环,最值数已经冒出。
譬如:
8 6 9 3 2 7
8 6 9 3 2 7
8 6 9 2 3 7
8 6 2 9 3 7
8 2 6 9 3 7
2 8 6 9 3 7
….
2 3 8 6 9 7
….
2 3 6 8 7 9
….
2 3 6 7 8 9
….
2 3 6 7 8 9
程序代码如下:
以下是引用片段:
Private Sub mpPaiXu(a() As Double, sheng As Boolean)
'a为需要排序的数组,sheng为True则为升序排列,为False,则为降序排列。
Dim i As Integer, j As Integer
Dim temp As Double
Dim m As Integer
For i = LBound(a) To UBound(a) - 1 '进行n-1轮比较
For j = UBound(a) To i + 1 Step -1 '从n到i个元素两两进行比较
If sheng Then '若次序不对,马上进行交换
If a(j) < a(j - 1) Then
temp = a(j)
a(j) = a(j - 1)
a(j - 1) = temp
End If
Else
If a(j) > a(j - 1) Then
temp = a(j)
a(j) = a(j - 1)
a(j - 1) = temp
End If
End If
Next j '出了内循环,一轮排序结束
'最值元素冒到最上边
Next i
End Sub
调用该过程代码基本同上。
2、实战练习
1) 补充代码
下面是一个采用拉锯式排序法对数组元素按升序进行排序的程序,所谓“拉锯式排序法”是这一遍把最小的元素从下到上送到最上的位置,下一遍则是从上到下把最大的元素送到最下的位置。
以下是引用片段:
Option Base 1
Private Sub Command1_Click()
Dim a(10) As Integer,i As Integer
For i = 1 To 10
a(i) = Int(Rnd * 10)+1
Text1 = Text1 & Str(a(i))
Next i
Call shaker_sort(a)
For i = 1 To 10
Text2 = Text2 & Str(a(i))
Next i
End Sub
Private Sub Shaker_sort(k() As Integer)
Dim i As Integer,c As Integer,d As Integer
Dim t As Integer
c = 1
d = (1)
Do
For (2) Step-1
If k(i=1)>k(i) Then
t = k(i-1):k(i-1) = k(i):k(i) = t
End If
Next i
(3)
For i = c+1 To d
If (4) Then
t = k(i-1):k(i-1) = k(i):k(i) = t
End If
Next i
d = d-1
Loop While (5)
End Sub
2) 编程题
把文本框输入的字符串按降序添加到列表框中。
是否可以解决您的问题?
VB排序问题
Option Explicit
Private Sub Form_Click()
Dim a(1 To 10) As Integer, iMin%, n%, j%, t%, i%
Picture1.Print "产生的随机数为:"
Picture2.Print "按降序排列为:"
For i = 1 To 10
a(i) = Int(Rnd * 90 + 10)
Picture1.Print a(i)
Next i
n = UBound(a) '你这个地方错了,真是大意呀!
'排序我用的冒泡法,你的那个方法在第一次循环的时候imin是0,用F8的单步运行就会发现错在这里了,因为你的数组是从1开始的。
For i = 1 To n - 1
For j = i To n
If a(i) < a(j) Then
t = a(i)
a(i) = a(i)
a(j) = t
End If
Next j
Next i
For i = LBound(a) To UBound(a)
Picture2.Print a(i)
Next
End Sub
vb程序设计,关于排序的问题
用冒泡排序
for i=1 to 5 do
for j=1 to 5-i do
if worker(j).pay>worker(j+1).pay then
t=work(j)
work(j)=work(j+1)
work(j+1)=t
end if
next j
next i