大家在学习VB.NET的时候,可能会发现其中文资料非常少,很难满足广大编程人员的需求。这就往往需要我们在依靠书本以及不断的实践去积累应用技巧,以达到学习的目的。那么今天要教给大家的是关于VB.NET冒泡排序的相关方法:#t#
VB.NET冒泡排序相关代码示例:
- Public Sub BubbleSort
(ByVal R As Integer()) - Dim i, j, temp As Integer
交换标志
- Dim exchange As Boolean
- For i = 0 To R.Length - 1
本趟VB.NET冒泡排序开始前,交换标志应为假
- exchange = False
- For j = R.Length -
2 To 0 Step -1
交换条件
- If R(j + 1) < R(j) Then
- temp = R(j + 1)
- R(j + 1) = R(j)
- R(j) = temp
发生了交换,故将交换标志置为真
- exchange = True
- End If
- Next
本趟VB.NET冒泡排序未发生交换,提前终止算法
- If Not exchange Then
- Exit For
- End If
- Next
- End Sub
选择VB.NET冒泡排序:
- Public Sub Sort()Sub Sort(ByVal list() As Integer)
- Dim min As Integer
- Dim i As Integer
- For i = 0 To list.Length - 1 - 1 Step +1
- min = i
- Dim j As Integer
- For j = i + 1 To list.Length - 1 Step +1
- If list(j) < list(min) Then
- min = j
- End If
- Next
- Dim t As Integer = list(min)
- list(min) = list(i)
- list(i) = t
- Next
- End Sub