凯凯学习了桶排序和冒泡排序,他发现桶排序占空间比较多而冒泡排序的效率又较低,于是他想到综合两个算法来对数据进行排序。凯凯对n个由二位数组成的数据进行排序,排序步骤如下:
1)以每个数的十位数来区分,放置到各个桶里(如图①);
2)分别对每个桶中的数据进行冒泡排序(如图②);
3)最后依次输出各个桶中的数据即可(如图③)。
Dim a(1 To 100) As Integer, b(1 To 100) As Integer
Dim c(0 To 10) As Integer Dim n As Integer
Private Sub Form_Load( )
'读取若干个二位数到a(i)中,并保存个数到n
End Sub
Private Sub Command1_Click()
Dim i As Integer, t As Integer
For i = 1 To n
c(a(i) \ 10) = c(a(i) \ 10) + 1
Next i
c(0) = 1 : c(10) = ①
For i = 1 To 9
c(i) = c(i) + c(i - 1)
Next i
For i = 1 To n
t = a(i) \ 10
c(t) = c(t) - 1
②
Next i
For i = 1 To 9
Call px(c(i), c(i + 1) – 1)
Next i
For i = 1 To n
List2.AddItem b(i)
Next i
End Sub
Sub px(x As Integer, y As Integer)
Dim t As Integer, i As Integer, j As Integer
For i =
For j = x To y - i
If b(j) > b(j + 1) Then
t = b(j): b(j) = b(j + 1): b(j + 1) = t
End If
Next j
Next i
End Sub