首页 > 编程语言 > 详细

C#实现简单的冒泡排序

时间:2018-01-25 16:41:08      阅读:225      评论:0      收藏:0      [点我收藏+]
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#实现简单的冒泡排序

原文:http://blog.51cto.com/12831900/2065060

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!