有一組正整數,基于冒泡排序對其中的數進行升序排序。排序后奇數在前,偶數在后。排序示例如下:
排序前 | 78 | 30 | 64 | 39 | 49 | 4 | 8 | 32 | 18 | 32 |
排序后 | 39 | 49 | 83 | 4 | 8 | 18 | 30 | 32 | 64 | 78 |
Const n=10
Dim d(1To n) As Integer
Private Sub Command1_Click ( ?。?br />Dim i As Integer,j As Integer,t As Integer
'讀取一組正整數,存儲在數組d中,代碼略
i=1
Do While i<=n-1
For j=n To i+1Step-1
If d(j) Mod 2=d(j-1)Mod 2Then
If

t=d(j):d(j)=d(j-1):d(j-1)=t
End If

t=d(j):d(j)=d(j-1):d(j-1)=t
End If
Next j
i=i+1
Loop
'依次輸出排序后的數據,代碼略
End Sub
【考點】冒泡排序算法及其實現.
【答案】見試題解答內容
【解答】
【點評】
聲明:本試題解析著作權屬菁優(yōu)網所有,未經書面同意,不得復制發(fā)布。
發(fā)布:2024/6/27 10:35:59組卷:17難度:0.4
相似題
-
1.有如下 Python 程序段:
運行后列表中的值可能是( ?。?/h2>發(fā)布:2024/12/18 21:0:2組卷:5難度:0.3 -
2.用VB編寫的函數bubble_sort是一個用冒泡方法實現排序的函數,其調用時需要三個參數:布爾類型參數sx來確定是升序還是降序,sx為True時為升序,否則為降序;整數型數組a
比如有一個整數型數組a(1 to 10)需要降序排序,可以直接這樣調用:
a
該函數VB代碼如下:
Function bubble_sort(sx As Boolean,a
Dim i,j,t As Integer
For i=1 To n-1
For j=
If
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
End Function
請將代碼補充完整:
①
②發(fā)布:2024/12/20 20:30:1組卷:23引用:2難度:0.3 -
3.用選擇排序法對一組數據進行升序排序,第一趟排序結束后的順序為2、6、9、3、7,則原始數據順序不可能的是( ?。?/h2>
發(fā)布:2024/12/18 23:0:1組卷:10難度:0.5