更正确的asp冒泡排序

时间:2008-10-07 12:22:41  来源:第二电脑网上收集  作者:

  第二电脑网导读: UBound(ary)   If I = UBound(ary) Then Exit For    If ary(I) > ary(I+1) Then     FirstValue = ary(I)     SecondValue = ary(I+1)     ary(I) = SecondValue     ary(I+1) = FirstValue     KeepChecking = TRUE    End...
  正文:

网上搜到的代码,千篇一律是这个

Function Sort(ary)
Dim KeepChecking,I,FirstValue,SecondValue
 KeepChecking = TRUE
Do Until KeepChecking = FALSE
 KeepChecking = FALSE
 For I = 0 to UBound(ary)
  If I = UBound(ary) Then Exit For
   If ary(I) > ary(I+1) Then
    FirstValue = ary(I)
    SecondValue = ary(I+1)
    ary(I) = SecondValue
    ary(I+1) = FirstValue
    KeepChecking = TRUE
   End If
 Next
Loop
 Sort = ary
End Function

存在错误。。。。。。

测试一下就知道

s="11,3,1"
s=sort(split(s,","))
for i=0 to ubound(s)
response.write s(i) & "<br>"
next

打印结果是

1

11

 3

 

正确的function是:

function sort(ary)
ck=true
do Until ck = false
 ck=false
 For f = 0 to UBound(ary) -1
  if clng(ary(f))>clng(ary(f+1)) then
   v1=clng(ary(f))
   v2=clng(ary(f+1))
   ary(f)=v2
   ary(f+1)=v1
   
   ck=true
  end if
 next
loop
sort=ary
end function

就差在一个clng()

但好笑的是,有些数组,用那个错误的sort函数是可以排正确的。


"更正确的asp冒泡排序"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Programming/ASP/2008-10-07/3477.html


关键字:

关于《更正确的asp冒泡排序》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td