日期:2014-05-17  浏览次数:20895 次

请教数组排序的问题!! 非常急啊~~~
我想对数组从小到大用冒泡法排序,写一个函数,然后再调用..
我写的是:  
<%Sub       popo(a(),e)  
        dim   i,n   ,keep  
      for   i       =       0     to   e-1    
      for   n       =       0     to     e-1-i      
            If       a(n)> a(n+1)       then      
                keep=a(n+1)      
              a(n+1)       =   a(n)      
                a(n)       =   keep      
            end       If      
        next      
next    
    End       Sub       %>

请问这个函数有没有写错呢???

调用是: <%call   popo(va(),z)%>
数组va(),和元素个数z已经赋值了
但是出错了:
"陣列索引超出範圍 "


------解决方案--------------------
首先第一行就有问题
<%Sub popo(a(),e)
<%Sub popo(a,e)
------解决方案--------------------
<%function popo(a(),e)
dim i,n ,keep
for i = 0 to e-1
for n = 0 to e-1-i
If a(n)> a(n+1) then
keep=a(n+1)
a(n+1) = a(n)
a(n) = keep
end If
popo=a()
next
next
End function %>

'写成函数,不是过程

------解决方案--------------------
Sub popo(A() )
Dim i, n, keep
For i = LBound(A) + 1 To UBound(A) '数组下限to上限
If A(i) < A(i - 1) Then '如果不符合从小到大的规则
n = i
Do While ChageValue(A(n), A(n - 1)) '把A(n)向上冒
n = n - 1
If n = LBound(A) Then Exit Do '如果达到数组第一个位置 exit do
Loop
End If

Next
End Sub

'这是一个比较并交换值的函数,如果X <Y,交换位置并返回真
Private Function ChageValue(x, y)
If x < y Then
x = x + y
y = x - y
x = x - y
ChageValue = True
Else
ChageValue = False
End If
End Function
------解决方案--------------------
楼主 你说了要写函数 怎么写过程了?
------解决方案--------------------
for i = 0 to e-1
for n = 0 to e-1-i

If a(n)> a(n+1) then
当i=0,n=e-1-i=e-1时
a(n+1)就是a(e)。显然你的数据最大下标是e-1
------解决方案--------------------
<script language= "vbscript ">
function popo(a)
'入口参数检查
if not isArray(a) then
popo=Array( "参数错误! ")
exit function
end if
dim i,j,temp
for i=0 to Ubound(a)-1
for j=i+1 to Ubound(a)
if a(i)> a(j) then
temp=a(i)
a(i)=a(j)
a(j)=temp
end if
next
next
popo=a
end function

'测试
dim arr