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

asp下几种常用排序算法

程序员文章站 2022-04-14 19:10:48
<% dim adata adata = array(3,2,4,1,6,0) call responsearray(adata, "原来顺序") call resp...
<%

dim adata
adata = array(3,2,4,1,6,0)

call responsearray(adata, "原来顺序")
call responsearray(selectsort(adata), "选择排序")
call responsearray(quicksort(adata), "快速排序")
call responsearray(insertsort(adata), "插入排序")
call responsearray(bubblesort(adata), "冒泡排序")


'选择排序
function selectsort(a_data)
dim i, j, k
dim bound, t
bound = ubound(a_data)

for i = 0 to bound-1
k = i
for j = i+1 to bound
if a_data(k) > a_data(j) then
k = j
end if
next
t = a_data(i)
a_data(i) = a_data(k)
a_data(k) = t
next

selectsort = a_data
end function


'快速排序
function quicksort(a_data)
dim i, j
dim bound, t
bound = ubound(a_data)

for i = 0 to bound-1
for j = i+1 to bound
if a_data(i) > a_data(j) then
t = a_data(i)
a_data(i) = a_data(j)
a_data(j) = t
end if
next
next

quicksort = a_data
end function


'冒泡排序
function bubblesort(a_data)
dim bound
bound = ubound(a_data)
dim bsorted, i, t
bsorted = false

do while bound > 0 and bsorted = false

bsorted = true
for i = 0 to bound-1
if a_data(i) > a_data(i+1) then
t = a_data(i)
a_data(i) = a_data(i+1)
a_data(i+1) = t
bsorted = false
end if
next
bound = bound - 1
loop

bubblesort = a_data
end function


'插入排序
function insertsort(a_data)
dim bound
bound = ubound(a_data)
dim i, j, t

for i = 1 to bound
t = a_data(i)
j = i
do while t<a_data(j-1) and j>0
a_data(j) = a_data(j-1)
j = j - 1
loop
a_data(j) = t
next

insertsort = a_data
end function

'输出数组
sub responsearray(a_data, str)
dim s
s = ""
response.write "<b>" & str & ":</b>"
for i = 0 to ubound(a_data)
s = s & a_data(i) & ","
next
s = left(s, len(s)-1)
response.write s
response.write "<hr>"
end sub
%>