测试生活

本Blog将不在更新,转入如下地址 http://123.127.140.50/ztest/default.aspx

导航

使用“冒泡法”对输入数字进行排序

Posted on 2007-11-07 10:30  张天利  阅读(302)  评论(0编辑  收藏  举报
原始代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void Sort(int[] list)//构造方法,对数组进行排序
    {
        int j = 1;

        int temp = 0;
        while (j < list.Length)
        {
            for (int i = 0; i < list.Length - j; i++)
                if (list[i] > list[i + 1])
                {
                    temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
                }
            j++;
        }
    }
   

    protected void Button1_Click(object sender, EventArgs e)
    {
        int[] iArrary = new int[] { Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text), Convert.ToInt32(TextBox3.Text), Convert.ToInt32(TextBox4.Text), Convert.ToInt32(TextBox5.Text) };
        _Default sh = new _Default();
        sh.Sort(iArrary);
        for (int m = 0; m < iArrary.Length; m++)
            Response.Write(iArrary[m]);
    }
}