|
|
Posted on
2006-04-14 15:26
laifangsong
阅读( 372)
评论()
收藏
举报
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace AspNetTest.Common
  {
 /**//// <summary>
/// Define_Delegate 的摘要说明。
/// </summary>
public class Define_Delegate_BubbleSort : System.Web.UI.Page
 {
private void Page_Load(object sender, System.EventArgs e)
 {
//以下写法也可以,但需要将 SortOrderASC和SortOrderDESC改为非静态方法
//DelegateSortApp dsa = new DelegateSortApp();
//dsa.SortASC();
//dsa.SortDESC();
DelegateSortApp.SortASC();
DelegateSortApp.SortDESC();
// 在此处放置用户代码以初始化页面
}

 Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
 {
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
 /**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
 {
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
public class BubbleSort
 {
public delegate bool Order(object first, object second);
public void Sort(Array table, Order order)
 {
if(order == null)
 {
throw new ArgumentException();
}
bool NotAllSort = true;
while(NotAllSort)
 {
int pass = 1;
NotAllSort = false;
for(int i=0; i<table.Length-pass; i++)
 {
if(order(table.GetValue(i), table.GetValue(i+1)))
 {
object temp = table.GetValue(i);
table.SetValue(table.GetValue(i+1), i);
table.SetValue(temp, i+1);
NotAllSort = true;
}
}
++pass;
}
}
}
public class DelegateSortApp
 {
 static int[] SortTable = {5,4,6,2,8,9,1,3,7,0};
public static void SortASC()
 {
HttpContext.Current.Response.Write("原数据:");
foreach(int i in SortTable)
 {
HttpContext.Current.Response.Write(i + " ");
}
HttpContext.Current.Response.Write("<br>");
HttpContext.Current.Response.Write("升序排序:");
BubbleSort bubbleSort = new BubbleSort();
BubbleSort.Order order = new BubbleSort.Order(SortOrderASC);
bubbleSort.Sort(SortTable, order);
foreach(int i in SortTable)
 {
HttpContext.Current.Response.Write(i + " ");
}
HttpContext.Current.Response.Write("<br>");
}
public static void SortDESC()
 {
HttpContext.Current.Response.Write("降序排序:");
BubbleSort bubbleSort = new BubbleSort();
BubbleSort.Order order = new BubbleSort.Order(SortOrderDESC);
bubbleSort.Sort(SortTable, order);
foreach(int i in SortTable)
 {
HttpContext.Current.Response.Write(i + " ");
}
}
public static bool SortOrderASC(object first, object second)
 {
int firstInt = (int)first;
int secondInt = (int)second;
return firstInt > secondInt;
}
public static bool SortOrderDESC(object first, object second)
 {
int firstInt = (int)first;
int secondInt = (int)second;
return firstInt < secondInt;
}
}
}

|