VB 选择排序进队数组元素是3的倍数的数排
时间: 2023-06-17 07:06:26 浏览: 95
以下是使用 VB 实现选择排序并将数组元素为3的倍数的数排在前面的示例代码:
```vb
Sub SelectSortWithCondition()
Dim arr() As Integer
arr = Array(6, 9, 2, 3, 12, 7, 18, 21)
Dim i As Integer
For i = 0 To UBound(arr) - 1
Dim minIndex As Integer
minIndex = i
Dim j As Integer
For j = i + 1 To UBound(arr)
If arr(j) < arr(minIndex) Then
minIndex = j
End If
Next j
' Swap the minimum value with the current value
If minIndex <> i Then
Dim temp As Integer
temp = arr(i)
arr(i) = arr(minIndex)
arr(minIndex) = temp
End If
Next i
' Move all elements that are multiples of 3 to the front of the array
Dim k As Integer
k = 0
For i = 0 To UBound(arr)
If arr(i) Mod 3 = 0 Then
Dim temp As Integer
temp = arr(i)
arr(i) = arr(k)
arr(k) = temp
k = k + 1
End If
Next i
' Print the sorted array
For i = 0 To UBound(arr)
Debug.Print arr(i)
Next i
End Sub
```
在上面的代码中,我们首先使用选择排序算法对数组进行排序,然后将所有元素为3的倍数的数移动到数组的前面。最后,我们打印了已排序的数组。
阅读全文