下标 |
1 |
2 |
3 |
4 |
5 |
值 |
5 |
3 |
3 |
2 |
4 |
第一步:统计数组 a 中每个值 a(i)出现的次数,存入 b(a(i)),统计完成后数组 b 的情况如下:
下标 |
1 |
2 |
3 |
4 |
5 |
值 |
0 |
1 |
2 |
1 |
1 |
第二步:从前向后,依次使数组 b 中的每个值等于其与前一项之和,这样数组 b(a(i))就代表了数组 a 中小于或等于 a(i)的元素个数,完成第二步后数组 b 如下:
下标 |
1 |
2 |
3 |
4 |
5 |
值 |
0 |
1 |
3 |
4 |
5 |
第三步:从后往前,依次将数组a中元素填入数组c中,将数组元素a(i)放在数组c的第b(a(i))项,如元素a(1)=5,应该放在数组c的b(5)位(即c(5))。每放一个元素就将b(a(i))递减;完成第三步后,数组c如下所示,已经完成升序排序,且相对位置不变,即原来a(2)的值3仍然在a(3)的值3前面。
下标 |
1 |
2 |
3 |
4 |
5 |
值 |
2 |
3 |
3 |
4 |
5 |
程序运行界面如右图所示:
实现上述功能的VB程序如下,回答以下问题:
Dim a(1 To 50) As Integer '存放学生的成绩
Dim sname(1 To 50) As String '存放学生姓名
Dim n As Integer
Private Sub Command1_Click
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
conn.ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
App.Path + "\stu.accdb"
conn.Open
strSQL = "SELECT * From stucj"
Set rs.ActiveConnection = conn
rs.Open strSQL
n = 0
List1.AddItem "姓 名" & " " & "成绩"
Do While Not rs.EOF
n = n + 1
a(n) = rs.Fields("cj")
sname(n) = rs.Fields("xm")
List1.AddItem sname(n) & " " & Str(a(n))
Loop
rs.Close
conn.Close
End Sub
Private Sub Command2_Click
Dim i As Integer, k As Integer
Dim b(0 To 100) As Integer
Dim c(1 To 50) As Integer, c1(1 To 50) As String 'c 和 c1 作为临时数组
For i = 1 To n
b(a(i)) = b(a(i)) + 1
Next
For i = 1 To 100
b(i) =
Next
For i = n To 1 Step -1
c(b(a(i))) = a(i)
c1(b(a(i))) = sname(i)
Next
For i = 1 To n '把临时数组 c 和 c1 中的数据放入 a 和 sname 数组
a(i) = c(i)
sname(i) = c1(i)
Next
List2.AddItem "姓 名" & " " & "成绩"
For i = 1 To n
List2.AddItem sname(i) & " " & Str(a(i))
Next
End Sub