asp:
<%
Dim aData
aData = Array(3,2,4,15,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 CLng(a_Data(k)) > CLng(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 CLng(a_Data(i)) > CLng(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 CLng(a_Data(i)) > CLng(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 CLng(t)<CLng(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
%>
c#:
1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11
12namespace suanfa
13{
14 /**//// <summary>
15 /// Sort 的摘要说明。
16 /// </summary>
17 public class Sort : System.Web.UI.Page
18 {
19 private void Page_Load(object sender, System.EventArgs e)
20 {
21 int i;
22 int [] arrSortData;
23 int[] arrData = new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
24 //输出未排序数据
25 Response.Write("<b>原来数据:<b><br>");
26 for(i=0; i<arrData.Length; i++)
27 {
28 Response.Write(arrData[i] + " ");
29 }
30 Response.Write("<hr>");
31 //输出冒泡排序结果
32 arrSortData = bubbleSort(arrData);
33 Response.Write("<b>冒泡排序结果:<b><br>");
34 for(i=0; i<arrSortData.Length; i++)
35 {
36 Response.Write(arrSortData[i] + " ");
37 }
38 Response.Write("<hr>");
39 //输出快速排序结果
40 arrSortData = quickSort(arrData);
41 Response.Write("<b>快速排序结果:<b><br>");
42 for(i=0; i<arrSortData.Length; i++)
43 {
44 Response.Write(arrSortData[i] + " ");
45 }
46 Response.Write("<hr>");
47 //输出插入排序结果
48 arrSortData = insertSort(arrData);
49 Response.Write("<b>插入排序结果:<b><br>");
50 for(i=0; i<arrSortData.Length; i++)
51 {
52 Response.Write(arrSortData[i] + " ");
53 }
54 Response.Write("<hr>");
55 //输出选择排序结果
56 arrSortData = selectSort(arrData);
57 Response.Write("<b>选择排序结果:<b><br>");
58 for(i=0; i<arrSortData.Length; i++)
59 {
60 Response.Write(arrSortData[i] + " ");
61 }
62 Response.Write("<hr>");
63 // 在此处放置用户代码以初始化页面
64 }
65
66 //冒泡排序
67 private int[] bubbleSort(int[] arrData)
68 {
69 if(arrData.Length <= 1)
70 {
71 return arrData;
72 }
73 bool bAllHaveSort = false;
74 for(int i=arrData.Length-1; i>0 && !bAllHaveSort; i--)
75 {
76 for(int j=0; j<i; j++)
77 {
78 bAllHaveSort = true;
79 if(arrData[j] > arrData[j+1])
80 {
81 int temp =
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11
12namespace suanfa
13{
14 /**//// <summary>
15 /// Sort 的摘要说明。
16 /// </summary>
17 public class Sort : System.Web.UI.Page
18 {
19 private void Page_Load(object sender, System.EventArgs e)
20 {
21 int i;
22 int [] arrSortData;
23 int[] arrData = new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
24 //输出未排序数据
25 Response.Write("<b>原来数据:<b><br>");
26 for(i=0; i<arrData.Length; i++)
27 {
28 Response.Write(arrData[i] + " ");
29 }
30 Response.Write("<hr>");
31 //输出冒泡排序结果
32 arrSortData = bubbleSort(arrData);
33 Response.Write("<b>冒泡排序结果:<b><br>");
34 for(i=0; i<arrSortData.Length; i++)
35 {
36 Response.Write(arrSortData[i] + " ");
37 }
38 Response.Write("<hr>");
39 //输出快速排序结果
40 arrSortData = quickSort(arrData);
41 Response.Write("<b>快速排序结果:<b><br>");
42 for(i=0; i<arrSortData.Length; i++)
43 {
44 Response.Write(arrSortData[i] + " ");
45 }
46 Response.Write("<hr>");
47 //输出插入排序结果
48 arrSortData = insertSort(arrData);
49 Response.Write("<b>插入排序结果:<b><br>");
50 for(i=0; i<arrSortData.Length; i++)
51 {
52 Response.Write(arrSortData[i] + " ");
53 }
54 Response.Write("<hr>");
55 //输出选择排序结果
56 arrSortData = selectSort(arrData);
57 Response.Write("<b>选择排序结果:<b><br>");
58 for(i=0; i<arrSortData.Length; i++)
59 {
60 Response.Write(arrSortData[i] + " ");
61 }
62 Response.Write("<hr>");
63 // 在此处放置用户代码以初始化页面
64 }
65
66 //冒泡排序
67 private int[] bubbleSort(int[] arrData)
68 {
69 if(arrData.Length <= 1)
70 {
71 return arrData;
72 }
73 bool bAllHaveSort = false;
74 for(int i=arrData.Length-1; i>0 && !bAllHaveSort; i--)
75 {
76 for(int j=0; j<i; j++)
77 {
78 bAllHaveSort = true;
79 if(arrData[j] > arrData[j+1])
80 {
81 int temp =