冒泡算法

来源:互联网 发布:mac怎么连接安卓手机 编辑:程序博客网 时间:2024/05/15 18:33

今天工作不是太忙,无意间翻博客看到冒泡排序的算法,热血沸腾也想写一个出来。好久没写过了。

代码贴出来:


  string words = "10,56,98,23,77,68,5";
        Response.Write("原始数据为:" + words+"<br/>");
        int[] array = new int[] { 10, 56, 98, 23, 77, 68, 5 };


        for (int i = 0; i < array.Length; i++)
        {
            for (int j = i + 1; j < array.Length; j++)
            {
                if (array[i] < array[j])
                {
                    int n = array[i];
                    array[i] = array[j];
                    array[j] = n;
                }
            }
        }

        string result = "";
        for (int i = 0; i < array.Length; i++)
            result += array[i] + ",";

        Response.Write("修改数据为:" + result);

原始数据为:10,56,98,23,77,68,5
修改数据为:98,77,68,56,23,10,5,

原创粉丝点击