.(填:冒泡排序/選擇排序) (2)在程序①和②橫線處填入適當(dāng)?shù)恼Z(yǔ)句或表達(dá)式,把程序補(bǔ)充完整. Dim a(1To 10)As Integer Function f(x As Integer)As Boolean '代碼略 End Function Private Sub Command1_Click( ?。?br />Dim n As Integer'n用于統(tǒng)計(jì)已經(jīng)產(chǎn)生的隨機(jī)整數(shù)個(gè)數(shù) Dim i As integer,j As Integer Dim x As Integer,k As Integer Randomize'初始化Rnd函數(shù) n=0 List1.Clear'清除列表框中內(nèi)容 List2.Clear Do While n<10 x=lnt(Rnd*999)+1'產(chǎn)生[1,999]范圍內(nèi)的隨機(jī)整數(shù) If Not f(x) Then n=n+1 a(n)=x List1.AddItem Str(
①a(n)
①a(n)
) End If Loop For i=1To 9'進(jìn)行排序 For j=10To i+1
②Step-1
②Step-1
If a(j)<a(j-1)Then k=a(j):a(j)=a(j-1):a(j-1)=k End If Next j Next i For i=1To 10 List2.AddItem Str(a(i)) Next i End Sub.
是待排序的數(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