【加试题】有一组正整数,基于冒泡排序对其中的数进行升序排序。排序后奇数在前,偶数在后。排序示例如下:
排序前 | 78 | 30 | 64 | 39 | 49 | 4 | 8 | 32 | 18 | 32 |
排序后 | 39 | 49 | 83 | 4 | 8 | 18 | 30 | 32 | 64 | 78 |
实现上述功能的VB程序如下,但加框处代码有误,请改正。
Const n = 10
Dim d(1 To n) As Integer
Private Sub Command1_Click()
Dim i As Integer, j As Integer, t As Integer
'读取一组正整数,存储在数组d中,代码略
i = 1
Do While i <= n - 1
For j = n To i + 1 Step -1
If d(j) Mod 2 = d(j - 1) Mod 2 Then
If Then '(1)
t = d(j): d(j) = d(j - 1): d(j - 1) = t
End If
'(2)
t = d(j): d(j) = d(j - 1): d(j - 1) = t
End If
Next j
i=i+1
Loop
'依次输出排序后的数据,代码略
End Sub
⑴ ⑵