1. <bdo id="8zfej"></bdo>
    <li id="8zfej"><meter id="8zfej"><th id="8zfej"></th></meter></li>

    南京中博教育

    全國咨詢電話:17368476151

    三分鐘了解中博教育
    當前位置:南京中博教育 > 學習園地 > 編程技巧

    C#版冒泡排序優化

    來源:未知? ? ? 作者:IT教育 ? ??

    之前寫了一個C#版冒泡排序的實現。由于之前的版本是用兩層for循環來實現的,這個版本的排序還是有進一步優化的空間的。 之前的排序: int temp = 0; for (int i = arr.Length - 1; i 0; i--) { fo
    之前寫了一個C#版冒泡排序的實現。由于之前的版本是用兩層for循環來實現的,這個版本的排序還是有進一步優化的空間的。
    之前的排序:
    int temp = 0;
    for (int i = arr.Length - 1; i > 0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                temp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = temp;
            }
        }
    }
    能夠看出,由于有兩層for循環的存在,使得整個排序必須要進行(n - 2)!次判斷,即使第一次產生的數組就是已經排好序的。
    對這個版本的冒泡排序進行優化:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace _0301數組的冒泡排序_優化
    {
        class Program
        {
            static void Main(string[] args)
            {
                int len = 0;        //數組大小
                int min = 0;        //數組下界
                int max = 0;        //數組上界
     
                Console.WriteLine("下面將產生一個隨機數組");
     
                //接收數組大小
                do
                {
                    Console.WriteLine("請輸入數組大小,它必須是一個小于等于10的正整數:");
                    len = int.Parse(Console.ReadLine());
                } while ((len <= 0) || (len > 10));
     
                //接收數組下界
                Console.WriteLine("請輸入數組下界,它必須是一個整數:");
                min = int.Parse(Console.ReadLine());
     
                //接收數組上界
                do
                {
                    Console.WriteLine("請輸入數組上界,它必須是一個整數,并且比數組小值大,且能讓數組容納{0}個數:", len);
                    max = int.Parse(Console.ReadLine());
                } while ((max <= min) || ((max - min + 1) < len));
     
                Console.WriteLine();
                Console.WriteLine("數組長度:{0}\n數組下界:{1}\n數組上界:{2}", len, min, max);
                Console.WriteLine("生成數組如下:");
     
                int iSeed = 6;
                Random ra = new Random(iSeed);
                int[] arr = new int[len];
                //打印原數組的值
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = ra.Next(min, max);
     
                    Console.Write(arr[i]);
                    if (i < arr.Length - 1)
                    {
                        Console.Write(",");
                    }
                    else
                    {
                        Console.WriteLine();
                    }
     
                }
     
                //開始進行數組排序
                Console.WriteLine("數組產生完畢,開始排序");
                Console.WriteLine();
     
                #region 原有排序,這種排序必須要比較(n -2)!次
                //int temp = 0;
                //for (int i = arr.Length - 1; i > 0; i--)
                //{
                //    for (int j = 0; j < i; j++)
                //    {
                //        if (arr[j] > arr[j + 1])
                //        {
                //            temp = arr[j + 1];
                //            arr[j + 1] = arr[j];
                //            arr[j] = temp;
                //        }
                //    }
                //}
                #endregion
     
                //這種排序多比較(n - 2)!次,但如果當前已是優排序結果則直接停止比較
                int temp = 0;
                bool swapped = true;
                do
                {
                    swapped = false;
     
                    for (int i = 0; i < arr.Length - 1; i++)
                    {
                        if (arr[i] > arr[i + 1])
                        {
                            temp = arr[i];
                            arr[i] = arr[i + 1];
                            arr[i + 1] = temp;
                            swapped = true;
                        }
                    }
     
                } while (swapped);
     
                //打印排序后的結果
                Console.WriteLine("排序后結果:");
     
                for (int i = 0; i < arr.Length; i++)
                {
                    Console.Write(arr[i]);
                    if (i < arr.Length - 1)
                    {
                        Console.Write(",");
                    }
                    else
                    {
                        Console.WriteLine();
                    }
     
                }
                Console.WriteLine("程序結束");
                Console.ReadKey();
            }
        }
    }
    這個版本的排序將外層循環改成了do...while()實現,并設置一個標識變量swapped,如果本次內部循環未進行一次數據置換則說明數組序列已實現優,立刻結束外層循環。這種排序多比較(n - 2)!次,但如果當前已是優排序結果則直接停止比較。

    其實外層循環是for循環也可以實現這種效果,但相對來說do...while()寫起來更優雅一些。

    分享到:
    近期文章

    搶試聽名額

    名額僅剩66名

    教育改變生活

    WE CHANGE LIVES

    主站蜘蛛池模板: 久久婷婷五月综合色丁香| 久久99国产综合精品女同| 天天av天天翘天天综合网| 六月婷婷国产精品综合| 色综合久久精品亚洲国产| 狠狠色成人综合首页| 亚洲欧洲国产成人综合在线观看| 一本色道久久综合狠狠躁| 伊人yinren6综合网色狠狠| 国产成人综合野草| 亚洲AV人无码综合在线观看| 小说区综合区首页| 91精品国产综合久久四虎久久无码一级 | 2020国产精品亚洲综合网| 久久综合亚洲色HEZYO国产| 色欲久久久久久综合网精品| 亚洲国产综合91精品麻豆| 久久综合久久鬼色| 伊人久久大香线蕉综合5g| 婷婷五月综合激情| 亚洲国产精品综合久久2007| 狼狼综合久久久久综合网| 一本久久知道综合久久| 伊人激情久久综合中文字幕| 一本大道久久a久久综合| 一本久久综合亚洲鲁鲁五月天| 久久桃花综合桃花七七网| 亚洲欧美国产国产综合一区| 色综合久久精品中文字幕首页 | 久久婷婷色香五月综合激情| 久久综合久久自在自线精品自| 一本色道久久综合狠狠躁| 久久久久久久综合| 成人综合久久精品色婷婷| 亚洲综合无码一区二区痴汉| 亚州AV综合色区无码一区| 天天做天天爱天天爽综合区| 国产成人综合精品一区| 狠狠色丁香婷婷综合激情| 人人妻人人狠人人爽天天综合网| 亚洲综合视频在线|