namespace BubbleSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numberList = { 3, 1, 5, 9, 7 };
            int i, j, temp;

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

            foreach(int number in numberList)
            {
                Console.Write("{0} ", number);
            }
            Console.ReadKey();
        }
    }
}

由小排到大或由大排到小,只要換將紅色換成>或<就行了.

arrow
arrow
    文章標籤
    c# bubble sort sample
    全站熱搜
    創作者介紹
    創作者 痞客興 的頭像
    痞客興

    痞客興的部落格

    痞客興 發表在 痞客邦 留言(0) 人氣()