dim StrA() as string
StrA = Split(str1,",")
For intI = 0 To UBound(StrA)
...
Next比如监视StrA中有StrA(0) = 1,StrA(1) = 2,Str(2) = 1
我需要把数组元素分别从小到大排列,即StrA(0) = 1,StrA(1) = 1,Str(2) = 2
请问怎么实现?
StrA = Split(str1,",")
For intI = 0 To UBound(StrA)
...
Next比如监视StrA中有StrA(0) = 1,StrA(1) = 2,Str(2) = 1
我需要把数组元素分别从小到大排列,即StrA(0) = 1,StrA(1) = 1,Str(2) = 2
请问怎么实现?
Private Sub Command1_Click()
Dim StrA() As String, str1 As String
Dim intI As Integer, intJ As Integer
Dim temp
str1 = "1,2,1,3,1,4"
StrA = Split(str1, ",")
For intI = 0 To UBound(StrA)
For intJ = intI To UBound(StrA)
If StrA(intI) > StrA(intJ) Then
temp = StrA(intI)
StrA(intI) = StrA(intJ)
StrA(intJ) = temp
End If
Next
Next
'比如监视StrA中有StrA(0) = 1,StrA(1) = 2,Str(2) = 1
'我需要把数组元素分别从小到大排列,即StrA(0) = 1,StrA(1) = 1,Str(2) = 2
'请问怎么实现?End Sub
dim StrA() as string
dim tmp as long
StrA = Split(str1,",")
For intI = 0 To UBound(StrA)-1
For intJ=intI+1 to UBound(StrA)
if StrA(intI)>StrA(intJ) then
tmp=StrA(intI)
StrA(intI)=StrA(intJ)
strA(intJ)=tmp
end if
next
Next
dim StrA() as string
dim tmp as long
StrA = Split(str1,",")
For intI = 0 To UBound(StrA)-1
For intJ=intI+1 to UBound(StrA)
if StrA(intI)>StrA(intJ) then
tmp=StrA(intI)
StrA(intI)=StrA(intJ)
strA(intJ)=tmp
end if
next
Next
Option Explicit
Global Const ZERO = 0
Global Const ASCENDING_ORDER = 0
Global Const DESCENDING_ORDER = 1Global gIterations As Long
Global BiJiaoCiShu As LongSub BubbleSort(MyArray(), ByVal nOrder As Integer) '冒泡算法
Dim Index
Dim TEMP
Dim NextElement '当前元素号(就是数组中的维数) NextElement = ZERO
Do While (NextElement < UBound(MyArray)) '当当前元素号小于数组总维数时循环
Index = UBound(MyArray) '取数组总维数
Do While (Index > NextElement) '当数组总维数大于当前元素号时循环
If nOrder = ASCENDING_ORDER Then '判断升序还是降序
If MyArray(Index) < MyArray(Index - 1) Then '判断INDEX号元素和他前一个元素大小
TEMP = MyArray(Index) '这三行
MyArray(Index) = MyArray(Index - 1) '是交换
MyArray(Index - 1) = TEMP '这二个元素
End If
BiJiaoCiShu = BiJiaoCiShu + 1
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Index - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = TEMP
End If
BiJiaoCiShu = BiJiaoCiShu + 1
End If
Index = Index - 1 '元素号-1,往前一个元素
gIterations = gIterations + 1
Loop
NextElement = NextElement + 1 '往后一个元素
gIterations = gIterations + 1
LoopEnd SubSub Bucket(MyArray(), ByVal nOrder As Integer) '基数排序法又称桶子法
Dim Index
Dim NextElement
Dim TheBucket NextElement = LBound(MyArray) + 1 '取数组最小维数+1,也就是第二个元素号
While (NextElement <= UBound(MyArray)) '从第二个元素号到最后一个元素号
TheBucket = MyArray(NextElement) '取值,当基数
Index = NextElement
Do '一个无条件循环,由内部退出
If Index > LBound(MyArray) Then '判断元素号
BiJiaoCiShu = BiJiaoCiShu + 1
If nOrder = ASCENDING_ORDER Then
If TheBucket < MyArray(Index - 1) Then '跟前一个数比较
MyArray(Index) = MyArray(Index - 1) '当前元素值换成前一个的值
Index = Index - 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If TheBucket >= MyArray(Index - 1) Then
MyArray(Index) = MyArray(Index - 1)
Index = Index - 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
gIterations = gIterations + 1
Loop
MyArray(Index) = TheBucket '上面循环得到的元素号的值=基数值
NextElement = NextElement + 1
gIterations = gIterations + 1
WendEnd SubSub Heap(MyArray()) '堆积排序法
Dim Index
Dim Size
Dim TEMP Size = UBound(MyArray) '取数组下标
Index = 1
While (Index <= Size) '从1到上面的下标
Call HeapSiftup(MyArray(), Index) '传:数组,第几个
Index = Index + 1
BiJiaoCiShu = BiJiaoCiShu + 1
gIterations = gIterations + 1
Wend Index = Size 'INDEX取下标
While (Index > 0) '递减循环
TEMP = MyArray(0)
MyArray(0) = MyArray(Index)
MyArray(Index) = TEMP
Call HeapSiftdown(MyArray(), Index - 1) '传:数组,第几个
Index = Index - 1
BiJiaoCiShu = BiJiaoCiShu + 1
gIterations = gIterations + 1
WendEnd Sub
Sub HeapSiftdown(MyArray(), M)
Dim Index
Dim Parent
Dim TEMP Index = 0
Parent = 2 * Index Do While (Parent <= M)
If (Parent < M And MyArray(Parent) < MyArray(Parent + 1)) Then
Parent = Parent + 1
End If If MyArray(Index) >= MyArray(Parent) Then
Exit Do
End If TEMP = MyArray(Index)
MyArray(Index) = MyArray(Parent)
MyArray(Parent) = TEMP
Index = Parent
Parent = 2 * Index gIterations = gIterations + 1
BiJiaoCiShu = BiJiaoCiShu + 1
Loop
End SubSub HeapSiftup(MyArray(), M)
Dim Index
Dim Parent
Dim TEMP Index = M
Do While (Index > 0)
Parent = Int(Index / 2) If MyArray(Parent) >= MyArray(Index) Then
Exit Do
End If
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Parent)
MyArray(Parent) = TEMP Index = Parent
gIterations = gIterations + 1
BiJiaoCiShu = BiJiaoCiShu + 1
Loop
End Sub
Sub Insertion(MyArray(), ByVal nOrder As Integer) '插入排序
Dim Index
Dim TEMP
Dim NextElement
NextElement = LBound(MyArray) + 1 '取第2个号
While (NextElement <= UBound(MyArray)) '循环到最后一个号
Index = NextElement '取当前号,给下面循环用
Do
If Index > LBound(MyArray) Then '判断当前号是否大于最小下标
BiJiaoCiShu = BiJiaoCiShu + 1
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Index - 1) Then
TEMP = MyArray(Index) '这三行
MyArray(Index) = MyArray(Index - 1) '交换
MyArray(Index - 1) = TEMP '数据
Index = Index - 1 '当前号减1,也就是往推一个数
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Index - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - 1)
MyArray(Index - 1) = TEMP
Index = Index - 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
gIterations = gIterations + 1
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
WendEnd SubSub QuickSort(MyArray(), L, R) '快速排序
Dim i, j, X, Y i = L '上标
j = R '下标
X = MyArray((L + R) / 2) '二分法,中间那个值
While (i <= j)
BiJiaoCiShu = BiJiaoCiShu + 1
While (MyArray(i) < X And i < R) '第一个往后向中间比
i = i + 1
BiJiaoCiShu = BiJiaoCiShu + 1
Wend
While (X < MyArray(j) And j > L) '最后一个往前向中间比
j = j - 1
BiJiaoCiShu = BiJiaoCiShu + 1
Wend
If (i <= j) Then '交换
Y = MyArray(i)
MyArray(i) = MyArray(j)
MyArray(j) = Y
i = i + 1
j = j - 1
End If
gIterations = gIterations + 1
Wend
If (L < j) Then Call QuickSort(MyArray(), L, j) '递归
If (i < R) Then Call QuickSort(MyArray(), i, R) '递归End SubSub Selection(MyArray(), ByVal nOrder As Integer) '选择排序
Dim Index
Dim Min
Dim NextElement
Dim TEMP NextElement = 0
While (NextElement < UBound(MyArray)) '循环从0到最后一个编号
Min = UBound(MyArray) '最后一个编号
Index = Min - 1 '倒数第二个编号
While (Index >= NextElement) '倒着循环
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Min) Then '判断
Min = Index '取编号
End If
BiJiaoCiShu = BiJiaoCiShu + 1
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Min) Then
Min = Index
End If
BiJiaoCiShu = BiJiaoCiShu + 1
End If
Index = Index - 1
gIterations = gIterations + 1
Wend
TEMP = MyArray(Min) '交换
MyArray(Min) = MyArray(NextElement) '值
MyArray(NextElement) = TEMP '
NextElement = NextElement + 1
gIterations = gIterations - 1
WendEnd SubSub ShellSort(MyArray(), ByVal nOrder As Integer) '希尔排序
Dim Distance
Dim Size
Dim Index
Dim NextElement
Dim TEMP Size = UBound(MyArray) - LBound(MyArray) + 1
Distance = 1 While (Distance <= Size)
Distance = 2 * Distance
Wend Distance = (Distance / 2) - 1
While (Distance > 0)
NextElement = LBound(MyArray) + Distance
While (NextElement <= UBound(MyArray))
Index = NextElement
Do
If Index >= (LBound(MyArray) + Distance) Then
BiJiaoCiShu = BiJiaoCiShu + 1
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Index - Distance) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - Distance)
MyArray(Index - Distance) = TEMP
Index = Index - Distance
gIterations = gIterations + 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Index - Distance) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index - Distance)
MyArray(Index - Distance) = TEMP
Index = Index - Distance
gIterations = gIterations + 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend
Distance = (Distance - 1) / 2
gIterations = gIterations + 1
Wend
End Sub
Private Sub Command2_Click()
Dim StrA() As String, str1 As String
Dim intI As Integer, intJ As Integer
Dim temp
Dim Idx As Long
str1 = "1,2,1,3,1,4"
StrA = Split(str1, ",")
For intI = 0 To UBound(StrA)
Idx = intI
For intJ = intI + 1 To UBound(StrA)
If StrA(Idx) > StrA(intJ) Then Idx = intJ
Next
If Idx <> intI Then
temp = StrA(intI)
StrA(intI) = StrA(Idx)
StrA(Idx) = temp
End If
Next
Debug.Print Join(StrA)
End Sub
StrA = Split(str1, ",")List1.Clear
For i = 0 To UBound(StrA)
List1.AddItem StrA(i)
Next For i = 0 To UBound(StrA)
StrA(i) = List1.List(i)
Next