下面这段代码是我经常用的快速排序代码,以前在VB6上使用,后来转换到VB.net上用,分享给大家,代码没有问题,可以直接复制过去就可以调用使用.
这是一段快速冒泡排序程序,一维数组,按照字符串进行排序StrComp(vTempVal, arrData(lTmpHi), CompareMethod.Text) ,如果需要按照其他方式排序可以去修改这个比较方式.
''' <summary>
''' Quick Sort
''' </summary>
''' <param name="arrData">A variant pointing to an array to be sorted.</param>
''' <param name="Low">LBounds(arrData) low number of elements in the array. if default Low=0,Low=LBound(arrData)</param>
''' <param name="Hi">UBounds(arrData) high number of elements in the array. if default Hi=0,Hi=UBounds(arrData)</param>
''' <param name="Descending">Ascending order or descending order</param>
''' <remarks></remarks>
Public Shared Sub _QuickSort(ByRef arrData() As Object, Optional ByVal Low As Integer = 0, Optional ByVal Hi As Integer = 0, Optional ByVal Descending As Boolean = False)
' ---------------------------------------------------------
'
' Syntax: QuickSort TmpAray(), Low, Hi
'
' Parameters:
' arrData - A variant pointing to an array to be sorted.
' Low - LBounds(arrData) low number of elements in the array
' Hi - UBounds(arrData) high number of elements in the array
'
' NOTE: I start my arrays with one and not zero.
' Make the appropriate changes to suit your code.
' ---------------------------------------------------------
' ---------------------------------------------------------
' Test to see if an array was passed
' ---------------------------------------------------------
If Not IsArray(arrData) Then Exit Sub
' ---------------------------------------------------------
' Define local variables
' ---------------------------------------------------------
Dim lTmpLow As Integer
Dim lTmpHi As Integer
Dim lTmpMid As Integer
Dim vTempVal As Object
Dim vTmpHold As Object
' ---------------------------------------------------------
' Initialize local variables
' ---------------------------------------------------------
Low = IIf(Low = 0, LBound(arrData), Low)
lTmpLow = Low
Hi = IIf(Hi = 0, UBound(arrData), Hi)
lTmpHi = Hi
' ---------------------------------------------------------
' Leave if there is nothing to sort
' ---------------------------------------------------------
If Hi <= Low Then Exit Sub
' ---------------------------------------------------------
' Find the middle to start comparing values
' ---------------------------------------------------------
lTmpMid = (Low + Hi) \ 2
' ---------------------------------------------------------
' Move the item in the middle of the array to the
' temporary holding area as a point of reference while
' sorting. This will change each time we make a recursive
' call to this routine.
' ---------------------------------------------------------
vTempVal = arrData(lTmpMid)
' ---------------------------------------------------------
' Loop until we eventually meet in the middle
' ---------------------------------------------------------
If Descending = False Then
Do While (lTmpLow <= lTmpHi)
' Always process the low end first. Loop as long
' the array data element is less than the data in
' the temporary holding area and the temporary low
' value is less than the maximum number of array
' elements.
'Do While (arrData(lTmpLow).ToString < vTempVal.ToString And lTmpLow < Hi)
' lTmpLow = lTmpLow + 1
'Loop
Do While (StrComp(arrData(lTmpLow), vTempVal, CompareMethod.Text) < 0 And lTmpLow < Hi)
lTmpLow = lTmpLow + 1
Loop
' Now, we will process the high end. Loop as long
' the data in the temporary holding area is less
' than the array data element and the temporary high
' value is greater than the minimum number of array
' elements.
'Do While (vTempVal.ToString < arrData(lTmpHi).ToString And lTmpHi > Low)
' lTmpHi = lTmpHi - 1
'Loop
Do While (StrComp(vTempVal, arrData(lTmpHi), CompareMethod.Text) < 0 And lTmpHi > Low)
lTmpHi = lTmpHi - 1
Loop
' if the temp low end is less than or equal
' to the temp high end, then swap places
If (lTmpLow <= lTmpHi) Then
vTmpHold = arrData(lTmpLow) ' Move the Low value to Temp Hold
arrData(lTmpLow) = arrData(lTmpHi) ' Move the high value to the low
arrData(lTmpHi) = vTmpHold ' move the Temp Hod to the High
lTmpLow = lTmpLow + 1 ' Increment the temp low counter
lTmpHi = lTmpHi - 1 ' Dcrement the temp high counter
End If
Loop
Else
Do While (lTmpLow <= lTmpHi)
' Always process the low end first. Loop as long
' the array data element is less than the data in
' the temporary holding area and the temporary low
' value is less than the maximum number of array
' elements.
'Do While (arrData(lTmpLow).ToString > vTempVal And lTmpLow < Hi)
' lTmpLow = lTmpLow + 1
'Loop
Do While (StrComp(arrData(lTmpLow), vTempVal, CompareMethod.Text) > 0 And lTmpLow < Hi)
lTmpLow = lTmpLow + 1
Loop
' Now, we will process the high end. Loop as long
' the data in the temporary holding area is less
' than the array data element and the temporary high
' value is greater than the minimum number of array
' elements.
'Do While (vTempVal > arrData(lTmpHi).ToString And lTmpHi > Low)
' lTmpHi = lTmpHi - 1
'Loop
Do While (StrComp(vTempVal, arrData(lTmpHi), CompareMethod.Text) > 0 And lTmpHi > Low)
lTmpHi = lTmpHi - 1
Loop
' if the temp low end is less than or equal
' to the temp high end, then swap places
If (lTmpLow <= lTmpHi) Then
vTmpHold = arrData(lTmpLow).ToString ' Move the Low value to Temp Hold
arrData(lTmpLow) = arrData(lTmpHi) ' Move the high value to the low
arrData(lTmpHi) = vTmpHold ' move the Temp Hod to the High
lTmpLow = lTmpLow + 1 ' Increment the temp low counter
lTmpHi = lTmpHi - 1 ' Dcrement the temp high counter
End If
Loop
End If
' ---------------------------------------------------------
' If the minimum number of elements in the array is
' less than the temp high end, then make a recursive
' call to this routine. I always sort the low end
' of the array first.
' ---------------------------------------------------------
If (Low < lTmpHi) Then
_QuickSort(arrData, Low, lTmpHi, Descending)
End If
' ---------------------------------------------------------
' If the temp low end is less than the maximum number
' of elements in the array, then make a recursive call
' to this routine. The high end is always sorted last.
' ---------------------------------------------------------
If (lTmpLow < Hi) Then
_QuickSort(arrData, lTmpLow, Hi, Descending)
End If
End Sub