[算法]冒泡排序

using System;

namespace ConsoleApp3
{
    class TestClass
    {
        static void Main(string[] args)
        {
            int[] int_arrArray = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            //int[] int_arrArray = new int[10] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
            BubbleSort(int_arrArray);

            for (int k = 0; k < int_arrArray.Length; k++)
            {
                Console.WriteLine(int_arrArray[k]);
            }
            Console.ReadKey();
        }

        static void BubbleSort(int[] intArray)
        {
            int temp = 0;
            bool swapped;
            for (int i = 0; i < intArray.Length; i++)
            {
                swapped = false;
                for (int j = 0; j < intArray.Length - 1 - i; j++)
                    if (intArray[j] < intArray[j + 1])
                    {
                        temp = intArray[j];
                        intArray[j] = intArray[j + 1];
                        intArray[j + 1] = temp;
                        if (!swapped)
                            swapped = true;
                    }
                if (!swapped)
                    return;
            }
        }

    }
}

 

posted @ 2022-10-24 12:00  SairenjiHaruna  阅读(10)  评论(0编辑  收藏  举报