冒泡排序

来源:互联网 发布:java类声明 编辑:程序博客网 时间:2024/06/04 00:41

好像跑起来没啥问题~~~~~大笑

 class Program    {        static void Main(string[] args)        {            int[] beforeSort = new int[] { 5, 3, 8, 6, 9, 1, 4, 2, 7 };            Console.Write("排序前:");            for (int i = 0; i < beforeSort.Length; i++)            {                Console.Write(beforeSort[i].ToString());            }            Console.Write("\r\n排序后:");            int[] afterSort = BubbleSort(beforeSort);            for (int j = 0; j < afterSort.Length; j++)            {                Console.Write(afterSort[j].ToString());            }            Console.ReadKey();        }        static int[] BubbleSort(int[] BeforeSort)        {            int[] sortArr = (int[])BeforeSort.Clone();            for (int i = 0; i < sortArr.Length; i++)            {                for (int j = 0; j < sortArr.Length - 1; j++)                {                    int temp = 0;                    if (sortArr[j] > sortArr[j + 1])                    {                        temp = sortArr[j];                        sortArr[j] = sortArr[j + 1];                        sortArr[j + 1] = temp;                    }                }            }            return sortArr;        }


0 0
原创粉丝点击