⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2.asp

📁 用aspvbScript实现数据结构中的排序算法
💻 ASP
字号:
<%@language=vbscript%>
<%option explicit%>
<%
		dim R(11),W(2)
		R(0)=request("data1")
		R(1)=request("data2")
		R(2)=request("data3")
		R(3)=request("data4")
		R(4)=request("data5")
		R(5)=request("data6")
		R(6)=request("data7")
		R(7)=request("data8")
		R(8)=request("data9")
		R(9)=request("data10")
		R(10)=request("data11")
		R(11)=request("data12")
		W(0)=6
		W(1)=3
		W(2)=1
		select case request("paixu")'根据第一个网页传递的参数,选择所要求的排序算法
			case "InsertSort":
				InsertSort R,12
			case "ShellSort":
				ShellSort R,12,W,3
			case "SelectSort":
				SelectSort R,12
			case "HeapSort":
				HeapSort R,12
			case "BubbleSort":
				BubbleSort R,12
			case "QuickSort":
				response.Write("快速排序算法排序过程如下所示:")
				response.write "<br>"
				QuickSort R,0,11
		End select'结束主程序
	Sub InsertSort(a,n)'直接插入排序算法子程序////////////////////////////////////////////////////////////////
		Dim i,j,temp,flag
			flag=1
			i=0
			response.Write("直接插入排序算法排序过程如下所示:")
			response.write "<br>"
		Do while i<n-1
			temp=a(i+1)
			j=i
				while (temp-a(j)<0 and j>0)'扫描数组,寻找小于a(j)的数
					a(j+1)=a(j)
					j=j-1
					a(j+1)=temp
				wend
				if (j=0 and temp-a(j)<0) then'为了消除越界,再加一条判断语句
					a(j+1)=a(j)
					a(j)=temp
				end if
			i=i+1
			response.Write("第"& i &"步:")
			StepOut a,n
		loop			
	End Sub
	Sub ShellSort(a,n,d,num)'希尔排序算法子程序///////////////////////////////////////////////////////////////
		Dim i,j,k,m,span,temp'用希尔排序算法对元素a(0)到a(n-1)排序,d(0)到d(num-1)为希尔增量值
			m=0
			response.Write("希尔排序算法排序过程如下所示:")
			response.write "<br>"
			Do while m<num'共num次循环
				span=d(m)'取本次的增量值
				k=0
					while k<span'共span个小组
						i=k
							while i<n-span'组内是直接插入排序,区别是每次增1而不是增span
								temp=a(i+span)
								j=i
								while (temp-a(j)<=0 and j>=span)
									a(j+span)=a(j)
									j=j-span
									a(j+span)=temp
								wend
								if (temp-a(j)<=0 and j>=0) then'为了消除越界,再加一条判断语句
									a(j+span)=a(j)
									a(j)=temp
								end if
								i=i+span
							wend
						k=k+1
					wend
				m=m+1
			response.Write("第"& m &"步:")
			StepOut a,n
			loop
	End sub
	Sub SelectSort(a,n)'直接选择排序算法子程序////////////////////////////////////////////////////////////////
		Dim i,j,x,temp
			i=0
			response.Write("直接选择排序算法排序过程如下所示:")
			response.write "<br>"
			Do while i<n-1
				x=i'设第i个数据元素的关键字最小
				j=i+1
					while j<n'寻找关键字最小的数据元素
						if a(j)-a(x)<0 then
							x=j'记住最小元素的下标
						end if
						j=j+1
					wend			
					if x<>i then'当最小元素的下标不为i时,交换位置
						temp=a(i)
						a(i)=a(x)
						a(x)=temp
					end if
				i=i+1
				response.Write("第"& i &"步:")
				StepOut a,n
			loop
	End sub
	Sub CreatHeap(a,n,h)'调整完全二叉树中某个非叶结点a(i)(i=(n-1)/2)使之满足最大堆定义
		Dim i,j,flag,temp
			i=h'i为要建堆的二叉树根结点下标
			j=2*i+1'j为i左孩子结点的下标
			temp=a(i)
			flag=0
		while (j<n-1 and flag<>1)'沿左右孩子中值较大者重复向上筛选
			if (j<n-1 and a(j)-a(j+1)<0) then'寻找左右孩子结点中的较大者,j为其下标
				j=j+1
			end if
			if(temp-a(j)>0) then'a(i)>a(j)
				flag=1'标记结束筛选条件
			else'否则把a(j)上移
				a(i)=a(j)
				i=j
				j=2*i+1
			end if
		wend
		a(i)=temp'把最初的a(i)赋给最后的a(j)
	End sub
	Sub InitCreatHeap(a,n)'初始化创建最大堆算法子程序
		Dim i
			i=Cint((n-1)/2)
		Do while i>=0
			CreatHeap a,n,i
			i=i-1
		loop
	end sub
	Sub HeapSort(a,n)'堆排序算法子程序////////////////////////////////////////////////////////////////////////
		Dim i,temp
			i=n-1
			InitCreatHeap a,n'初始化创建最大堆
			response.Write("堆排序算法排序过程如下所示:")
			response.write "<br>"
		Do while i>0'当前最大堆个数每次递减1
			temp=a(0)
			a(0)=a(i)'把堆顶a(0)元素与当前最大堆的最后一个元素交换
			a(i)=temp
			CreatHeap a,i,0'调整根结点满足最大堆
		i=i-1
		response.Write("第"& n-i-1 &"步:")
		StepOut a,n
		loop
		if a(0)-a(1)>0 then
			temp=a(0)
			a(0)=a(1)
			a(1)=temp
			response.Write("第"& n-i &"步:")
			StepOut a,n
		end if
	end sub
	Sub BubbleSort(a,n)'冒泡排序算法子程序////////////////////////////////////////////////////////////////////
		Dim i,j,flag,temp
			flag=1'flag变量用于标记本次排序动作是否有交换动作
			i=1
			response.Write("冒泡排序算法排序过程如下所示:")
			response.write "<br>"
			Do while (i<n and flag=1)
				flag=0
					for j=0 to n-i-1'两两比较,找出最小的数据元素放在排序数组的第一位
						if a(j)-a(j+1)>0 then
							flag=1
							temp=a(j)
							a(j)=a(j+1)
							a(j+1)=temp
						end if
					next
				i=i+1
				response.Write("第"& i-1 &"步:")
				StepOut a,n	
			loop				
	End sub
	Sub QuickSort(a,iLow,iHigh)'快速排序算法子程序////////////////////////////////////////////////////////////
		Dim i,j,x,temp'用递归方法对数据元素a(iLow)到a(iHigh)进行快速排序
			i=iLow
			j=iHigh
			x=a(Cint((iLow+iHigh)/2))'取中间的元素为标准数据元素
			StepOut a,12
			Do
				while(a(i)-x<0 and i<iHigh)'在数组的左端扫描
					i=i+1
				Wend
				while(x-a(j)<0 and j>iLow)'在数组的右端扫描
					j=j-1
				Wend
				IF i<=j Then
					temp=a(i)
					a(i)=a(j)
					a(j)=temp
					i=i+1
					j=j-1
				End If
			Loop while i<=j
			If iLow<j  then'对左端子集合进行递归
				QuickSort a,iLow,j
			End If
			If i<iHigh  then'对右端子集合进行递归
				QuickSort a,i,iHigh
			End If
	End Sub
	Sub StepOut(a,n)'每步输出所要调用子程序
		Dim i
			For i=0 to n-1
				response.Write(a(i)) &"&nbsp;"
			Next
				response.write "<br>"
	End Sub
%>
<html>
<head>
<title>排序算法</title>
</head>
<body background="back.gif">
<form>
排序最终结果如下所示:<br>
  <input type="text" name="data1" size="5" value="<%response.Write(R(0))%>">
  <input type="text" name="data2" size="5" value="<%response.Write(R(1))%>">
  <input type="text" name="data3" size="5" value="<%response.Write(R(2))%>">
  <input type="text" name="data4" size="5" value="<%response.Write(R(3))%>">
  <input type="text" name="data5" size="5" value="<%response.Write(R(4))%>">
  <input type="text" name="data6" size="5" value="<%response.Write(R(5))%>">
  <input type="text" name="data7" size="5" value="<%response.Write(R(6))%>">
  <input type="text" name="data8" size="5" value="<%response.Write(R(7))%>">
  <input type="text" name="data9" size="5" value="<%response.Write(R(8))%>">
  <input type="text" name="data10" size="5" value="<%response.Write(R(9))%>">
  <input type="text" name="data11" size="5" value="<%response.Write(R(10))%>">
  <input type="text" name="data12" size="5" value="<%response.Write(R(11))%>"><br><br>
<input type="BUTTON" value="查看源代码" style="cursor:hand" onClick= 'window.location = "view-source:" + window.location.href' name="BUTTON">
</form>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -