• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

利用集合进行数组的排序

发布: 2007-7-01 21:48 | 作者: admin | 来源: | 查看: 18次 | 进入软件测试论坛讨论

领测软件测试网

       笔者在学习VB中发现利用集合可以进行排序,优化后比冒泡法快得多。下面是完整的函数代码,演示了如何进行数组的升序或降序排列。

 

Option Explicit
Dim X(100) As Double
Dim y(100) As String
Private Sub Command1_Click() ´演示数字排序
Dim a(100) As Double, z(100) As String, i As Long
For i = 0 To 100
a(i) = X(i) ´复制数组
z(i) = CStr(X(i)) ´转化为字符串数组
Next
MsgBox Join(z, ","), 64, "原始数组" ´显示原始顺序

 

NUMBERSORT X, "up"
For i = 0 To 100
z(i) = CStr(X(i)) ´转化为字符串数组
Next
MsgBox Join(z, ","), 64, "按数字升序排序后数组" ´显示排序结果

 

NUMBERSORT a, "down"
For i = 0 To 100
z(i) = CStr(a(i)) ´转化为字符串数组
Next
MsgBox Join(z, ","), 64, "按数字降序排序后数组" ´显示排序结果

End Sub
Sub NUMBERSORT(ByRef a() As Double, Optional sort As String = "up") ´数字排序
Dim min As Long, max As Long, num As Long, first As Long, last As Long, temp As Long, all As New Collection, steps As Long
min = LBound(a)
max = UBound(a)
all.Add a(min)
steps = 1
For num = min + 1 To max
last = all.Count
If a(num) < CDbl(all(1)) Then all.Add a(num), BEFORE:=1: GoTo nextnum ´加到第一项
If a(num) > CDbl(all(last)) Then all.Add a(num), AFTER:=last: GoTo nextnum ´加到最后一项


first = 1
Do While last > first + 1 ´利用DO循环减少循环次数
temp = (last + first) \ 2
If a(num) > CDbl(all(temp)) Then
first = temp
Else
last = temp
steps = steps + 1
End If
Loop
all.Add a(num), BEFORE:=last ´加到指定的索引

nextnum:
steps = steps + 1
Next
For num = min To max
If sort = "UP" Or sort = "up" Then a(num) = CDbl(all(num - min + 1)): steps = steps + 1 ´升序
If sort = "DOWN" Or sort = "down" Then a(num) = CDbl(all(max - num + 1)): steps = steps + 1 ´降序
Next
MsgBox "本数组共经过 " & steps & "步实现" & IIf(sort = "UP" Or sort = "up", "升序", "降序") & "排序!", 64, "INFORMATION"
Set all = Nothing
End Sub

 

Sub STRINGSORT(ByRef a() As String, Optional sort As String = "UP") ´字符串排序
Dim min As Long, max As Long, num As Long, first As Long, last As Long, temp As Long, all As New Collection, steps As Long
min = LBound(a)
max = UBound(a)
all.Add a(min)
steps = 1
For num = min + 1 To max

first = 1
last = all.Count
If a(num) < all(1) Then all.Add a(num), BEFORE:=1: GoTo nextnum ´加到第一项
If a(num) > all(last) Then all.Add a(num), AFTER:=last: GoTo nextnum ´加到最后一项


Do While last > first + 1 ´利用DO循环减少循环次数
temp = (last + first) \ 2
If a(num) > all(temp) Then
first = temp
Else
last = temp
steps = steps + 1
End If
Loop
all.Add a(num), BEFORE:=last ´加到指定的索引

nextnum:
steps = steps + 1
Next
For num = min To max
If sort = "UP" Or sort = "up" Then a(num) = all(num - min + 1): steps = steps + 1 ´升序
If sort = "DOWN" Or sort = "down" Then a(num) = all(max - num + 1): steps = steps + 1 ´降序
Next
MsgBox "本数组共经过 " & steps & "步实现" & IIf(sort = "UP" Or sort = "up", "升序", "降序") & "排序!", 64, "INFORMATION"
Set all = Nothing
End Sub


Private Sub Command2_Click() ´演示字符串排序
Dim z(100) As String, i As Long ´复制数组
For i = 0 To 100
z(i) = y(i)
Next


MsgBox Join(y, ","), 64, "原始数组" ´显示原始顺序

 

STRINGSORT y, "UP"
MsgBox Join(y, ","), 64, "按字符串升序排序后数组" ´显示排序结果

 


STRINGSORT z, "DOWN"
MsgBox Join(z, ","), 64, "按字符串降序排序后数组" ´显示排序结果
End Sub

Private Sub Command3_Click() ´ 排序计时
Dim a(3000) As String, i As Long, starttime As Long, endtime As Long
For i = 0 To 3000
a(i) = Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) ´生成随机6字符的字符串
Next
starttime = Timer ´计时开始
STRINGSORT a
endtime = Timer ´计时结束
MsgBox "排序共耗时 " & endtime - starttime & " 秒!"
End Sub

Private Sub Form_Load()
Randomize
Dim i As Long
For i = 0 To 100
X(i) = Format(Rnd * 1000, "0.00")
y(i) = Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65) & Chr(Int(Rnd * 26) + 65)
Next
End Sub


 


延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网