首页 > 其他 > 详细

选择排序和冒泡排序

时间:2014-07-16 19:17:32      阅读:226      评论:0      收藏:0      [点我收藏+]
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

选择排序和冒泡排序

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

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