zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    MySQL索引原理及慢查询优化
    MySQL单表百万数据记录分页性能优化
    linux下crontab命令的使用
    php递归读取目录
    php实现函数重载
    php数组常见的几种遍历方法
    ArtTemplate 使用笔记
    打算换工作的伙伴们,快来看啦,各种职位,随便挑咯...
    看看国外的javascript题目,你能全部做对吗?(分享)
    逛园子,看到个练习题,小试了一把(淘宝ued的两道小题)
  • 原文地址:https://www.cnblogs.com/password1/p/5870772.html
Copyright © 2011-2022 走看看