Console-算法-冒泡排序法|倒水法

ylbtech-Arithmetic:Console-算法-冒泡排序法|倒水法
 
1.A,案例

-- ========================================================
-- ylb:算法
-- type:冒泡法排序
-- thankyou:sunshine, 谢谢你的默默付出
-- 10:50 2012-04-06
-- ========================================================

1.B,解决方案

 1.B.1,算法一

using System;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// ylb:冒泡法排序(倒水法)
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            int[] array = { 12, 1, 5, 20, 4 };

            int temp = 0;
            for (int i = 0; i < array.Length; i++)
            {

                for (int j = i + 1; j < array.Length; j++)
                {

                    //升序
                    if (array[i] > array[j])
                    {
                        temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }

            //输出排序后的结果
            for (int i = 0; i < array.Length; i++)
            {

                Console.Write(array[i]+"\t");
            }
            Console.WriteLine("\n");

            foreach (int item in array)
            {

                Console.Write(item + "\t");
            }

        }
    }
}

1.B.2,算法二 【操作对象内部储存变量】

using System;

namespace ConsoleApplication2
{
    class Program
    {
        public static void Sort(int[] myArray)
        {

            // 取长度最长的词组 -- 冒泡法
            for (int j = 1; j < myArray.Length; j++)
            {
                for (int i = 0; i < myArray.Length - 1; i++)
                {
                    // 如果 myArray[i] > myArray[i+1] ,则 myArray[i] 上浮一位
                    if (myArray[i] > myArray[i + 1])
                    {
                        int temp = myArray[i];
                        myArray[i] = myArray[i + 1];
                        myArray[i + 1] = temp;
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            int[] myArray = new int[] { 20, 10, 8, 30, 5, 1, 2, 22 };
            
            Sort(myArray);

            for (int m = 0; m < myArray.Length; m++)
            {
                Console.WriteLine(myArray[m]);
            }
        }
    }
}
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2013-02-22 13:58  ylbtech  阅读(392)  评论(0编辑  收藏  举报