首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 經典排序算法實現:氣泡、選擇、插入與希爾排序完整代碼解析

     編輯:關於C語言

前言

演算法是程式設計的基石,無論語言如何更迭,核心邏輯始終不變。本文將展示四種經典排序演算法——氣泡排序、選擇排序、插入排序與希爾排序的 C# 完整實現代碼,並附帶測試範例,幫助初學者理解排序邏輯與 C# 語法的結合應用。

一、氣泡排序

氣泡排序透過重複遍歷陣列,比較相鄰元素並交換位置,將較大元素逐步「浮」至陣列末端。雖然時間複雜度為 O(n²),但邏輯直觀,適合教學入門。

核心代碼

using System;

namespace BubbleSorter
{
    public class BubbleSorter
    {
        public void Sort(int[] list)
        {
            int i, j, temp;
            bool done = false;
            j = 1;
            while ((j < list.Length) && (!done))
            {
                done = true;
                for (i = 0; i < list.Length - j; i++)
                {
                    if (list[i] > list[i + 1])
                    {
                        done = false;
                        temp = list[i];
                        list[i] = list[i + 1];
                        list[i + 1] = temp;
                    }
                }
                j++;
            }
        }
    }

    public class MainClass
    {
        public static void Main()
        {
            int[] iArray = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
            BubbleSorter sorter = new BubbleSorter();
            sorter.Sort(iArray);
            for (int m = 0; m < iArray.Length; m++)
                Console.Write("{0} ", iArray[m]);
            Console.WriteLine();
        }
    }
}

二、選擇排序

選擇排序每次從未排序區間找出最小值,放到已排序區間末尾。同樣為 O(n²) 複雜度,但交換次數較氣泡排序少。

核心代碼

using System;

namespace SelectionSorter
{
    public class SelectionSorter
    {
        private int min;
        public void Sort(int[] list)
        {
            for (int i = 0; i < list.Length - 1; i++)
            {
                min = i;
                for (int j = i + 1; j < list.Length; j++)
                {
                    if (list[j] < list[min])
                        min = j;
                }
                int t = list[min];
                list[min] = list[i];
                list[i] = t;
            }
        }
    }

    public class MainClass
    {
        public static void Main()
        {
            int[] iArray = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
            SelectionSorter ss = new SelectionSorter();
            ss.Sort(iArray);
            for (int m = 0; m < iArray.Length; m++)
                Console.Write("{0} ", iArray[m]);
            Console.WriteLine();
        }
    }
}

三、插入排序

插入排序將陣列分為「已排序」與「未排序」兩部分,每次從未排序區取出一個元素,插入到已排序區的適當位置。對於小規模或近乎有序的資料效率極高。

核心代碼

using System;

namespace InsertionSorter
{
    public class InsertionSorter
    {
        public void Sort(int[] list)
        {
            for (int i = 1; i < list.Length; i++)
            {
                int t = list[i];
                int j = i;
                while ((j > 0) && (list[j - 1] > t))
                {
                    list[j] = list[j - 1];
                    --j;
                }
                list[j] = t;
            }
        }
    }

    public class MainClass
    {
        public static void Main()
        {
            int[] iArray = new int[] { 1, 13, 3, 6, 10, 55, 98, 2, 87, 12, 34, 75, 33, 47 };
            InsertionSorter ii = new InsertionSorter();
            ii.Sort(iArray);
            for (int m = 0; m < iArray.Length; m++)
                Console.Write("{0}", iArray[m]);
            Console.WriteLine();
        }
    }
}

四、希爾排序

希爾排序是插入排序的改進版,透過「間距」將陣列分組進行插入排序,逐步縮小間距至 1,大幅提升大規模資料排序效能,平均複雜度可達 O(n log² n)。

核心代碼

using System;

namespace ShellSorter
{
    public class ShellSorter
    {
        public void Sort(int[] list)
        {
            int inc;
            for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
            for (; inc > 0; inc /= 3)
            {
                for (int i = inc + 1; i <= list.Length; i += inc)
                {
                    int t = list[i - 1];
                    int j = i;
                    while ((j > inc) && (list[j - inc - 1] > t))
                    {
                        list[j - 1] = list[j - inc - 1];
                        j -= inc;
                    }
                    list[j - 1] = t;
                }
            }
        }
    }

    public class MainClass
    {
        public static void Main()
        {
            int[] iArray = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
            ShellSorter sh = new ShellSorter();
            sh.Sort(iArray);
            for (int m = 0; m < iArray.Length; m++)
                Console.Write("{0} ", iArray[m]);
            Console.WriteLine();
        }
    }
}

總結與建議

四種排序演算法各有適用場景:

  • 氣泡/選擇/插入排序:適合小規模資料(n < 50)或教學示範
  • 希爾排序:中等規模資料的實用選擇,無需遞迴且空間複雜度 O(1)
  • 實際專案中建議直接使用 Array.Sort() 或 List<T>.Sort(),底層採用 Introspective Sort(混合快速排序、堆積排序、插入排序),效能最優

學習演算法不僅是為了面試,更是培養邏輯思維與優化意識。建議動手重寫上述代碼,嘗試加入泛型支援(T[] where T : IComparable<T>)以提升複用性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved