码迷,mamicode.com
首页 > 其他好文 > 详细

选择排序和冒泡排序

时间:2014-07-16 19:17:52      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   for   div   

static void Main(string[] args)
        {
            int[] arr = new int[] { 11,1, 9, 4, 6, 8, 6, 11, 30 };
 
            printMM(arr);
            SelectSort(arr);
            printMM(arr);
 
            int[] arr1 = new int[] { 11, 1, 9, 4, 6, 8, 6, 11, 30 };
 
            printMM(arr1);
            BoubleSort(arr1);
            printMM(arr1);
        }
 
        static void printMM(int[] inputarr)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < inputarr.Length; i++)
            {
                sb.Append(inputarr[i]).Append(",");
            }
            Console.WriteLine(sb.ToString());
        }
 
        /// <summary>
        /// 选择排序
        /// </summary>
        /// <param name="inputArr"></param>
        static void SelectSort(int[] inputArr)
        {
            for (int i = 0; i < inputArr.Length-2; i++)
            {
                for (int j = i+1; j <= inputArr.Length-1; j++)
                {
                    if(inputArr[i]>inputArr[j])
                    {
                        int temp = inputArr[i];
                        inputArr[i] = inputArr[j];
                        inputArr[j] = temp;
                    }
                }
            }
        }
 
        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <param name="inputArr"></param>
        static void BoubleSort(int[] inputArr)
        {
            for (int i = 0; i < inputArr.Length - 1; i++)
            {
                bool isExchange = false;
                for (int j = inputArr.Length-2; j >=i; j--)
                {
                    if (inputArr[j] > inputArr[j+1])
                    {
                        int temp = inputArr[j+1];
                        inputArr[j+1] = inputArr[j];
                        inputArr[j] = temp;
 
                        isExchange = true;
                    }
                }
                if (!isExchange)
                    break;
            }
        }

 

选择排序和冒泡排序,布布扣,bubuko.com

选择排序和冒泡排序

标签:style   blog   color   os   for   div   

原文地址:http://www.cnblogs.com/zhxm/p/3844772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!