c# 自定义排序类(冒泡、选择、插入、希尔、快速、归并、堆排序等)

using System;
using System.Text;

namespace HuaTong.General.Utility
{
    /// <summary>
    /// 自定义排序类
    /// </summary>
    public class Sorter
    {
        /// <summary>
        /// 交换元素位置
        /// </summary>
        public static void Swap<T>(ref T[] arr, int i, int j)
        {
            T temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        /// <summary>
        /// 冒泡排序
        /// </summary>
        public static T[] BubbleSort<T>(T[] arr) where T : IComparable
        {
            for (int i = arr.Length - 1; i > 0; --i)
            {
                for (int j = 0; j < i; ++j)
                {
                    if (arr[j].CompareTo(arr[j + 1]) > 0)
                    {
                        Swap(ref arr, j, j + 1);
                    }
                }
            }
            return arr;
        }

        /// <summary>
        /// 选择排序
        /// </summary>
        public static T[] SelectionSort<T>(T[] arr) where T : IComparable
        {
            for (int i = 0; i < arr.Length; ++i)
            {
                int index = i;
                for (int j = i + 1; j < arr.Length; ++j)
                {
                    if (arr[j].CompareTo(arr[index]) < 0) index = j;
                }
                Swap(ref arr, i, index);
            }
            return arr;
        }

        /// <summary>
        /// 插入排序
        /// </summary>
        public static T[] InsertionSort<T>(T[] arr) where T : IComparable
        {
            for (int i = 1; i < arr.Length; ++i)
            {
                int j = i;
                T value = arr[i];
                while (j > 0 && arr[j - 1].CompareTo(value) > 0)
                {
                    arr[j] = arr[j - 1];
                    --j;
                }
                arr[j] = value;
            }
            return arr;
        }

        /// <summary>
        /// 希尔排序
        /// </summary>
        public static T[] ShellSort<T>(T[] arr) where T : IComparable
        {
            for (int step = arr.Length >> 1; step > 0; step >>= 1)
            {
                for (int i = 0; i < step; ++i)
                {
                    for (int j = i + step; j < arr.Length; j += step)
                    {
                        int k = j;
                        T value = arr[j];
                        while (k >= step && arr[k - step].CompareTo(value) > 0)
                        {
                            arr[k] = arr[k - step];
                            k -= step;
                        }
                        arr[k] = value;
                    }
                }
            }
            return arr;
        }

        /// <summary>
        /// 快速排序
        /// </summary>
        public static T[] QuickSort<T>(T[] arr, int startIndex, int endIndex) where T : IComparable
        {
            int i, j;
            T x, y;

            i = (startIndex < 0 ? 0 : startIndex);
            j = (endIndex > arr.Length - 1 ? arr.Length - 1 : endIndex);
            if (startIndex > endIndex) return null;
            x = arr[(i + j) / 2];

            while (i <= j)
            {
                while (arr[i].CompareTo(x) < 0 && i < endIndex)
                {
                    i = i + 1;
                }
                while (x.CompareTo(arr[j]) < 0 && j > startIndex)
                {
                    j = j - 1;
                }
                if (i <= j)
                {
                    y = arr[i];
                    arr[i] = arr[j];
                    arr[j] = y;
                    i = i + 1;
                    j = j - 1;
                }
            }

            if (startIndex < j) QuickSort(arr, startIndex, j);
            if (i < endIndex) QuickSort(arr, i, endIndex);

            return arr;
        }

        /// <summary>
        /// 归并排序
        /// </summary>
        public static T[] MergeSort<T>(T[] arr, int startIndex, int endIndex, T[] arrF) where T : IComparable
        {
            startIndex = (startIndex < 0 ? 0 : startIndex);
            endIndex = (endIndex > arr.Length - 1 ? arr.Length - 1 : endIndex);
            arrF = new T[arr.Length];
            if (startIndex >= endIndex) return null;
            int m = (startIndex + endIndex) >> 1;
            MergeSort(arr, startIndex, m, arrF);
            MergeSort(arr, m + 1, endIndex, arrF);
            for (int i = startIndex, j = startIndex, k = m + 1; i <= endIndex; ++i)
            {
                arrF[i] = arr[(k > endIndex || j <= m && arr[j].CompareTo(arr[k]) < 0) ? j++ : k++];
            }
            for (int i = startIndex; i <= endIndex; ++i) arr[i] = arrF[i];

            return arr;
        }

        /// <summary>
        /// 堆排序
        /// </summary>
        public static T[] HeapSort<T>(T[] arr) where T : IComparable
        {
            for (int i = 1; i < arr.Length; ++i)
            {
                for (int j = i, k = (j - 1) >> 1; k >= 0; j = k, k = (k - 1) >> 1)
                {
                    if (arr[k].CompareTo(arr[j]) >= 0) break;
                    Swap(ref arr, j, k);
                }
            }
            for (int i = arr.Length - 1; i > 0; --i)
            {
                Swap(ref arr, 0, i);
                for (int j = 0, k = (j + 1) << 1; k <= i; j = k, k = (k + 1) << 1)
                {
                    if (k == i || arr[k].CompareTo(arr[k - 1]) < 0) --k;
                    if (arr[k].CompareTo(arr[j]) <= 0) break;
                    Swap(ref arr, j, k);
                }
            }
            return arr;
        }
    }
}
原文地址:https://www.cnblogs.com/password1/p/5870772.html