修改正确的asp冒泡排序
程序员文章站
2023-01-24 23:27:59
网上搜到的代码,千篇一律是这个 function sort(ary) dim keepchecking,i,firstvalue,secondvalue...
网上搜到的代码,千篇一律是这个
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函数是可以排正确的。
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 的中文分词
下一篇: 文件名 正则表达式提取方法