程序=算法+数据结构
软件=程序+文档
我的理解,算法就是解决问题的方法和步骤,因为这世界充满了问题,所以可以说,算法无处不在。
Option Explicit
Sub 冒泡排序()
Dim arr, temp, x, y, t, k
t = Timer
arr = Range("a1:a10")
For x = 1 To UBound(arr) - 1
For y = x + 1 To UBound(arr) '只和当前数字下面的数进行比较
If arr(x, 1) > arr(y, 1) Then '如果它大于它下面某一个数字
temp = arr(x, 1)
arr(x, 1) = arr(y, 1)
arr(y, 1) = temp
End If
Next y
Next x
Range("b3").Resize(x) = ""
Range("b3").Resize(x) = arr
'Range("b2") = Timer - t
MsgBox k
End Sub
Sub 冒泡排序演示()
Dim arr, temp, x, y, t, k
For x = 1 To 9
Range("a" & x).Interior.ColorIndex = 3
For y = x + 1 To 10 '只和当前数字下面的数进行比较
Range("a" & y).Interior.ColorIndex = 4
If Cells(x, 1) > Cells(y, 1) Then '如果它大于它下面的某一个数字
temp = Cells(x, 1)
Cells(x, 1) = Cells(y, 1)
Cells(y, 1) = temp
2 End If
Range("a" & y).Interior.ColorIndex = xlNone
Next y
Range("a" & x).Interior.ColorIndex = xlNone
Next x
End Sub
Option Explicit
Sub 选择排序()
Dim arr, temp, x, y, t, iMax, k, k1, k2
t = Timer
arr = Range("a1:a10")
For x = UBound(arr) To 1 + 1 Step -1
iMax = 1 '最大的索引
For y = 1 To x
If arr(y, 1) > arr(iMax, 1) Then iMax = y
Next y
temp = arr(iMax, 1)
arr(iMax, 1) = arr(x, 1)
arr(x, 1) = temp
Next x
'Range("c3").Resize(UBound(arr)) = ""
'Range("c3").Resize(UBound(arr)) = arr
'Range("c2") = Timer - t
'MsgBox k1
End Sub
Sub 选择排序单元格演示()
Dim arr, temp, x, y, t, iMax, k, k1, k2
For x = 10 To 2 Step -1
iMax = 1
Range("a" & x).Interior.ColorIndex = 3
For y = 1 To x
Range("a" & y).Interior.ColorIndex = 4
If Cells(y, 1) > Cells(iMax, 1) Then
Range("a" & iMax).Interior.ColorIndex = xlNone
iMax = y
End If
Range("a" & y).Interior.ColorIndex = xlNone
Range("a" & iMax).Interior.ColorIndex = 6
Next y
temp = Cells(iMax, 1)
Cells(iMax, 1) = Cells(x, 1)
Cells(x, 1) = temp
Range("a" & x).Interior.ColorIndex = xlNone
Range("a" & iMax).Interior.ColorIndex = xlNone
Next x
End Sub