Bubble Sort (c#)

        static void Main(string[] args)
        {
            int[] array = { 23, 45, 16, 7, 42,100,2 };
            int length = array.Length - 1;
            bool isExchanged = false;
            for (int i = 0; i < length; i++)
            {
                for (int j = length; j > i; j--)
                {
                    if (array[j]< array[j - 1])
                    {
                        int temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                        isExchanged = true;
                    }
                }
                if (!isExchanged)
                    break;
            }
            foreach (int i in array)
            {
                Console.WriteLine(i);
            } Console.Read();
        }

posted @ 2011-08-15 15:30  从头儿再来  阅读(189)  评论(0编辑  收藏  举报