15.小王基于選擇排序算法編寫了一個VB程序,功能如下:讀取若干數據依次存儲在數組a中,并將數據分段排序,每段數據的元素個數及排序的次序要求依次存儲在數組b中。如圖,在文本框Text1中顯示數組a的原始數據,在文本框Text2中顯示每段數據的元素個數及排序次序要求(0表示升序、1表示降序);單擊“排序”按鈕Command1,根據要求輸出對每段數據進行排序的結果。實現上述功能的VB程序如下: Dim n As Integer Im a(1 To 100)As integer Dim b(1 To 100)As Integer Private Sub Form _Load( ) '讀取若干數據依次存儲到數組a中,并將數據元素的總個數仔儲到變量n中 '將每段數據的元素個數及排序的次序依次存儲到數組b中: 'b(1)、b(2)分別存儲第1段數據的元素個數,排序的次序, 'b(3)、b(4)分別存儲第2段數據的元素個數、排序的次序,… '代碼略 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)觀察代碼,排序后的數據輸出在對象
(用“,”間隔)。 (2)實現上述功能的VB程序如下,請在橫線處填入合適代碼。 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個整數保存到數組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( ) Then Exit Do ′在列表框List1中輸出這一輪合并后的數據,代碼略 Loop End sub