Sub BubbleSortNumbers(iArray As Variant)'冒泡
Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lTemp As Long
For lLoop1 = UBound(iArray) To LBound(iArray) Step -1
For lLoop2 = LBound(iArray) + 1 To lLoop1
If iArray(lLoop2 - 1) > iArray(lLoop2) Then
lTemp = iArray(lLoop2 - 1)
iArray(lLoop2 - 1) = iArray(lLoop2)
iArray(lLoop2) = lTemp
End If
Next lLoop2
Next lLoop1
End Sub
Sub SelectionSortNumbers(vArray As Variant)‘选择
Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lMin As Long
Dim lTemp As Long
For lLoop1 = LBound(vArray) To UBound(vArray) - 1
lMin = lLoop1
For lLoop2 = lLoop1 + 1 To UBound(vArray)
If vArray(lLoop2) < vArray(lMin) Then lMin = lLoop2
Next lLoop2
lTemp = vArray(lMin)
vArray(lMin) = vArray(lLoop1)
vArray(lLoop1) = lTemp
Next lLoop1
End Sub
Sub ShellSortNumbers(vArray As Variant)’Shell快速
Dim lLoop1 As Long
Dim lHold As Long
Dim lHValue As Long
Dim lTemp As Long
lHValue = LBound(vArray)
Do
lHValue = 3 * lHValue + 1
Loop Until lHValue > UBound(vArray)
Do
lHValue = lHValue / 3
For lLoop1 = lHValue + LBound(vArray) To UBound(vArray)
lTemp = vArray(lLoop1)
lHold = lLoop1
Do While vArray(lHold - lHValue) > lTemp
vArray(lHold) = vArray(lHold - lHValue)
lHold = lHold - lHValue
If lHold < lHValue Then Exit Do
Loop
vArray(lHold) = lTemp
Next lLoop1
Loop Until lHValue = LBound(vArray)
End Sub
1 : 直接选择排序
从所有数中找到最大值,然后跟第一个数交换,,然后从余下数中找到最大值,跟第2个交换。。
'// 10 个数
for i = 0 to 8
for j = i +1 to 9
if arr(i) < arr(j) then swap(i,j)
next j
next i
2: 快速排序
寻找一个中间数(一般可以取数序列中间位置数), 设置2个index,分别指向最左和最右
先从左边开始, 一旦发现left(index)>=mid 就停止,否则继续查找,然后从右边开始 一旦发现
right(index)<=mid 就停止,否则继续查找, 2个都找到, 就交换2个数
然后继续恢复左边查找, 停止的依据是left.index > right.index
这样数字以left.index(right.index)为分割符(前部分都小于等于中间数字,后部分都大于等于中间数),左边数序列跟右边数序列递归排序, 结合一起就是有序数列
public function sort(byval left as long, byval right as long, arr() as long)
dim old_left as long, old_right as long, mid as long
old_left = left: old_right = right
mid = arr((left + right) /2)
do while left <= right
do while arr(left)<mid : left = left+1 : loop
do while arr(right)>mid : right = right-1 : loop
loop
call sort(old_left, right)
call sort(left, old_right)
end
3: 冒泡排序
从第一个数开始跟相邻数比较,大的数上浮,小的下沉, 经过一轮循环,最大数浮到了顶端
然后从余下的数字继续开始浮动。
'// 10 个数
for i = 0 to 8
for j = 0 to 8-i
if arr(j) > arr(j+1) then swap(j,j+1)
next j
next i
'排序 冒泡
Private Function ArrSort_MP(arrLng() As Long) As Long
Dim lngI As Long
Dim lngJ As Long
Dim lngK As Long '记录最后交换位置
Dim lngU As Long
Dim lngTemp As Long
lngU = UBound(arrLng)
For lngI = 1 To lngU
lngK = lngI + 1
For lngJ = lngU To lngI Step -1
If arrLng(lngJ - 1) > arrLng(lngJ) Then
lngTemp = arrLng(lngJ - 1)
arrLng(lngJ - 1) = arrLng(lngJ)
arrLng(lngJ) = lngTemp
lngK = lngJ
End If
Next
lngI = lngK - 1
Next
End Function
'排序 选择
Private Function ArrSort_XZ(arrLng() As Long) As Long
Dim lngI As Long
Dim lngJ As Long
Dim lngU As Long
Dim lngTemp As Long
lngU = UBound(arrLng)
For lngI = 0 To lngU - 1
lngTemp = arrLng(lngI)
For lngJ = lngI + 1 To lngU
If lngTemp > arrLng(lngJ) Then
lngTemp = arrLng(lngJ)
End If
Next
arrLng(lngI) = lngTemp
Next