欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

修改正确的asp冒泡排序

程序员文章站 2022-05-28 16:26:27
网上搜到的代码,千篇一律是这个 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

打印结果是



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函数是可以排正确的。