是待排序的數(shù)據(jù),數(shù)據(jù)從a(1)開始存放;整數(shù)型參數(shù)n表示傳入的數(shù)組長(zhǎng)度,該函數(shù)的返回值也是一個(gè)整數(shù)型數(shù)組.所以調(diào)用此函數(shù)實(shí)現(xiàn)排序非常方便: 比如有一個(gè)整數(shù)型數(shù)組a(1 to 10)需要降序排序,可以直接這樣調(diào)用: a
=bubble_sort(False,a
,10) 該函數(shù)VB代碼如下: Function bubble_sort(sx As Boolean,a
As Integer,n As Integer) As Integer
Dim i,j,t As Integer For i=1 To n-1 For j=
Step-1 If
Then If a(j)<a(j-1)Then t=a(j):a(j)=a(j-1):a(j-1)=t Else 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 bubble_sort=a
實(shí)現(xiàn)上述功能的VB程序如下,請(qǐng)完成以下問題: Dim a(1To 8)As Single Dim n As Integer,i As Integer,x As Single Private Sub Form_Load ( ?。?br />Dim j As Integer,temp As Single ’輸入7位選手100米成績(jī),代碼略 ’將7位選手成績(jī)從小到大排序 For i=1To 6 For j=7To i+1Step-1 If a(j)<a(j-1)Then temp=a(j) a(j)=a(j-1) a(j-1)=temp End If Next j Next i ’將排序好的選手成績(jī)輸出到列表框List1 List1.Clear For i=1To 7 List1.AddItem Str(a(i)) Next i End Sub Private Sub Command1_Click ( ?。_定該遺漏選手的成績(jī)排名第幾位? x=Val(Text1.Text) i=1 Do While x>a(i) ① Loop Label4.Caption=“該選手位列第”+Str(i)+“名!” ’將該遺漏選手成績(jī)添加到適當(dāng)位置,使數(shù)組仍保持有序并輸出到列表框List2 For j=8To i+1Step-1 a(j)=a(j-1) Next j ② For i=1To 8 List2.AddItem a(i) Next i End Sub (1)Form Load事件處理過程中代碼采用的算法是