C#版冒泡排序法

 //冒泡排序法
    private void SortArray()
    {
        //从大到小排列
        int[] Arr = new int[] { 8, 4, 9, 5, 3, 2, 6, 7, 1 };
        for (int i = 1; i < Arr.Length; i++)
        {
            for (int j = 0; j < Arr.Length - 1; j++)
            {
                //如果 Arr[j]<Arr[j+1],则Arr[j]下沉一位
                if (Arr[j] < Arr[j + 1])
                {
                    int tmp = Arr[j];
                    Arr[j] = Arr[j + 1];
                    Arr[j + 1] = tmp;
                }
            }
        }
        for (int t = 0; t < Arr.Length; t++)
        {
            Response.Write(Arr[t].ToString() + "<br />");
        }

        //从小到大排列
       
        for (int i = 1; i < Arr.Length; i++)
        {
            for (int j = 0; j < Arr.Length - 1; j++)
            {
                //如果 Arr[j+1]>Arr[j],则Arr[j]上浮一位
                if (Arr[j] > Arr[j + 1])
                {
                    int tmp = Arr[j];
                    Arr[j] = Arr[j+1];
                    Arr[j + 1] = tmp;
                   
                }
            }
        }

        for (int t = 0; t < Arr.Length; t++)
        {
            Response.Write(Arr[t].ToString() + "<br />");
        }
    }

posted @ 2008-12-01 10:29  李帅斌-Memory  阅读(1091)  评论(3编辑  收藏  举报