C#实现简单的冒泡排序

C#实现简单的冒泡排序

1、C#代码下:

using System;

namespace ConsoleApplication1

{

    class Program

    {

        static void Main()

        {

            int[] arrSort = new int[] { 10, 8, 3, 5, 6, 7, 9 };//初始化排序数据

            Bubble_Sort(ref arrSort);//调用冒泡排序方法


            for (int i = 0; i < arrSort.Length; i++)//输出排序结果

            {

                Console.WriteLine("排序的结果为:{0}", arrSort[i]);

            }

            Console.ReadLine();//暂停输出窗口

        }

        /// <summary>

        /// C#实现简单的冒泡排序

        /// </summary>

        private static void Bubble_Sort(ref int[] arrSort)//ref表示引用型

        {

            int temp;//预先定义一个中间变量

            for (int i = 0; i < arrSort.Length; i++)

            {

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

                {

                    if (arrSort[j] < arrSort[i])//交换数据位置

                    {

                        temp = arrSort[j];

                        arrSort[j] = arrSort[i];

                        arrSort[i] = temp;

                    }

                }

            }

        }

    }

}


2、输出的结果如下:

C#实现简单的冒泡排序