15.小王基于選擇排序算法編寫了一個(gè)VB程序,功能如下:讀取若干數(shù)據(jù)依次存儲(chǔ)在數(shù)組a中,并將數(shù)據(jù)分段排序,每段數(shù)據(jù)的元素個(gè)數(shù)及排序的次序要求依次存儲(chǔ)在數(shù)組b中。如圖,在文本框Text1中顯示數(shù)組a的原始數(shù)據(jù),在文本框Text2中顯示每段數(shù)據(jù)的元素個(gè)數(shù)及排序次序要求(0表示升序、1表示降序);單擊“排序”按鈕Command1,根據(jù)要求輸出對(duì)每段數(shù)據(jù)進(jìn)行排序的結(jié)果。實(shí)現(xiàn)上述功能的VB程序如下: Dim n As Integer Im a(1 To 100)As integer Dim b(1 To 100)As Integer Private Sub Form _Load( ?。?br />'讀取若干數(shù)據(jù)依次存儲(chǔ)到數(shù)組a中,并將數(shù)據(jù)元素的總個(gè)數(shù)仔儲(chǔ)到變量n中 '將每段數(shù)據(jù)的元素個(gè)數(shù)及排序的次序依次存儲(chǔ)到數(shù)組b中: 'b(1)、b(2)分別存儲(chǔ)第1段數(shù)據(jù)的元素個(gè)數(shù),排序的次序, 'b(3)、b(4)分別存儲(chǔ)第2段數(shù)據(jù)的元素個(gè)數(shù)、排序的次序,… '代碼略 End Sub Private Sub Commandl Click Dim i As Integer,j As Integer,k As Integer,t As Integer Dim pb As Integer,endpos As Integer pb=1:endpos=b(1) For i=1 To n-1 If i=endpos Then pb=pb+2:i=endpos+1 ①
End If k=i For j=i+1 To endpos If Thenk=j Next j Ifk<>i Then ②
End If Next i Text3 Text=″″ Fori=1 To n Text3 Text-Text3 Text Str(a(i)) End Sub (1)觀察代碼,排序后的數(shù)據(jù)輸出在對(duì)象
(用“,”間隔)。 (2)實(shí)現(xiàn)上述功能的VB程序如下,請(qǐng)?jiān)跈M線處填入合適代碼。 Const n As Integer=10 Dim a(I To n+5)As integer Dim tmp(I To n+5)As Integer Private Sub Form Load ′讀取n個(gè)整數(shù)保存到數(shù)組a中,并在文本框 TextI中顯示,代碼略 End sub Function Position( low As Integer) As integer Dim i As Integer For i-low Ton-1 If①
Then Exit For Next i Position-=i End Function Function Merged As Boolean Dim i As Integer,flag As Boolean,pI As Integer.p2 As Integer Dim low I As Integer,highI As Integer,high2 As Integer flag False:lowl=1 Do While low I<n highl-Position(lowD) If highI-n Then Exit Do Else flag True high2-Position(highl+1) pl-lowl:p2-highl+I Do while②
If p2>high2 Or pl c-highl And a(pl)<a(p2)Then tmp(i)a(p2):pl=pl+1 tmp-a(p2):p2-p2+1 End If i=i+1 Loop For i=low I To high2 a(0)=tmp(o) Next i ③
Loop Merge=fIng End Function Private Sub Commandl _Click( ) Do While True Ir Not Merge( ?。?nbsp;Then Exit Do ′在列表框List1中輸出這一輪合并后的數(shù)據(jù),代碼略 Loop End sub