制作一个集合了绝大多数排序的DLL

记录下现在所流行的各种各样的排序,用于之后使用。暂时集成:插入排序,希尔排序,快排,堆排序,归并排序。

using System;
using System.Collections.Generic;
namespace Sort {
    static public class Sort {

        //插入排序
        public delegate Q Condition<T,Q> (T t) where Q:IComparable;
        static public void Insertion_Sort<T,Q> (T[] a,Condition<T,Q> condition) where Q:IComparable {
            int i,j;
            int n = a.Length;
            for (i = 1;i < n;i++) {
                T temp = a[i];
                for (j = i;j > 0 && condition(temp).CompareTo(condition(a[j - 1])) < 0;--j)
                    a[j] = a[j - 1];
                a[j] = temp;
            }
        }
        static public void Insertion_Sort<T,Q> (List<T> a,Condition<T,Q> condition) where Q:IComparable {
            int i,j;
            int n = a.Count;
            for (i = 1;i < n;i++) {
                T temp = a[i];
                for (j = i;j > 0 && condition(temp).CompareTo(condition(a[j - 1])) < 0;--j)
                    a[j] = a[j - 1];
                a[j] = temp;
            }
        }
        //希尔排序
        public static void Shell_Sort<T,Q> (T[] arr,Condition<T,Q> condition) where Q:IComparable {//希尔排序 也是插入排序的一种
            //增量gap,并逐步缩小增量  gap一般都是 /2/2这样操作
            for (int gap = arr.Length / 2;gap > 0;gap /= 2) {
                //从第gap个元素,逐个对其所在组进行直接插入排序操作
                for (int i = gap;i < arr.Length;i++) {
                    int j = i;
                    T temp = arr[j];
                    if (condition(temp).CompareTo(condition(arr[j - 1])) < 0) {
                        while (j - gap >= 0 && condition(temp).CompareTo(condition(arr[j - 1])) < 0) {
                            //移动法
                            arr[j] = arr[j - gap];//向上挪移
                            j -= gap;
                        }
                        arr[j] = temp;
                    }
                }
            }
        }
        public static void Shell_Sort<T,Q> (List<T> arr,Condition<T,Q> condition) where Q:IComparable {//希尔排序 也是插入排序的一种
            //增量gap,并逐步缩小增量  gap一般都是 /2/2这样操作
            for (int gap = arr.Count / 2;gap > 0;gap /= 2) {
                //从第gap个元素,逐个对其所在组进行直接插入排序操作
                for (int i = gap;i < arr.Count;i++) {
                    int j = i;
                    T temp = arr[j];
                    if (condition(temp).CompareTo(condition(arr[j - 1])) < 0) {
                        while (j - gap >= 0 && condition(temp).CompareTo(condition(arr[j - 1])) < 0) {
                            //移动法
                            arr[j] = arr[j - gap];//向上挪移
                            j -= gap;
                        }
                        arr[j] = temp;
                    }
                }
            }
        }

        //快排
        public static void Quick_Sort<T,Q> (T[] arr,Condition<T,Q> condition) where Q:IComparable {
            if (arr != null && arr.Length != 0) {
                QuickSort<T,Q>(arr,condition,0,arr.Length - 1);
            }
        }
        static void QuickSort<T,Q> (T[] arr,Condition<T,Q> condition,int left,int right) where Q:IComparable {//快速排序  确定一个key,然后两个指针和key进行比较,如果左指针小于key 左就++,如果右指针大于key 右指针就--,直到两者停下然后交换。
            if (left >= right) {
                return;
            }
            int begin = left;
            int end = right;
            int key = right;

            while (left < right) {
                while (left < right && condition(arr[left]).CompareTo(condition(arr[key])) <= 0)
                    left++;
                while (left < right && condition(arr[right]).CompareTo(condition(arr[key])) >= 0)
                    right--;
                if (left < right && condition(arr[left]).CompareTo(condition(arr[key])) != 0)
                    swap(ref arr[left],ref arr[right]);
            }
            if (condition(arr[left]).CompareTo(condition(arr[key])) != 0)
                swap(ref arr[left],ref arr[key]);
            QuickSort(arr,condition,begin,left - 1);
            QuickSort(arr,condition,left + 1,end);
        }

        public static void Quick_Sort<T,Q> (List<T> arr,Condition<T,Q> condition) where Q:IComparable {
            if (arr != null && arr.Count != 0) {
                QuickSort<T,Q>(arr,condition,0,arr.Count - 1);
            }
        }
        static void QuickSort<T,Q> (List<T> arr,Condition<T,Q> condition,int left,int right) where Q:IComparable {//快速排序  确定一个key,然后两个指针和key进行比较,如果左指针小于key 左就++,如果右指针大于key 右指针就--,直到两者停下然后交换。
            if (left >= right) {
                return;
            }
            int begin = left;
            int end = right;
            int key = right;

            while (left < right) {
                while (left < right && condition(arr[left]).CompareTo(condition(arr[key])) <= 0)
                    left++;
                while (left < right && condition(arr[right]).CompareTo(condition(arr[key])) >= 0)
                    right--;
                if (left < right && condition(arr[left]).CompareTo(condition(arr[key])) != 0)
                    swap(arr,left,right);
            }
            if (condition(arr[left]).CompareTo(condition(arr[key])) != 0)
                swap(arr,left,key);
            QuickSort(arr,condition,begin,left - 1);
            QuickSort(arr,condition,left + 1,end);
        }


        //堆排序
        static private void heapSort<T,Q> (T[] a,Condition<T,Q> condition,int n,int k) where Q:IComparable {//堆排序
            int k1 = 2 * k + 1;//左边
            int k2 = 2 * k + 2;//右边

            if (k1 >= n && k2 >= n)
                return;
            T a1;
            T a2;
            T a3;

            if (k2 < n) {
                a1 = a[k1];
                a2 = a[k2];
                a3 = a[k];
                if (condition(a3).CompareTo(condition(a1)) < 0 && condition(a3).CompareTo(condition(a2)) < 0)
                    return;
                if (condition(a1).CompareTo(condition(a2)) < 0) {
                    swap<T>(ref a[k1],ref a[k]);
                    heapSort(a,condition,n,k1);
                } else {
                    swap<T>(ref a[k2],ref a[k]);
                    heapSort(a,condition,n,k2);
                }
            } else {
                a1 = a[k1];
                a3 = a[k];
                if (condition(a3).CompareTo(condition(a1)) < 0)
                    return;
                if (condition(a1).CompareTo(condition(a3)) < 0) {
                    swap<T>(ref a[k1],ref a[k]);
                    heapSort(a,condition,n,k1);
                }
            }

        }
        static public void Heap_Sort<T,Q> (T[] a,Condition<T,Q> condition) where Q:IComparable {//堆排序
            for (int i = (a.Length - 1) / 2;i >= 0;i--)
                heapSort<T,Q>(a,condition,a.Length,i);
            int n = a.Length;
            List<T> aa = new List<T>();
            while (a.Length > 0 && condition(a[0]).CompareTo(condition(a[n - 1])) != 0) {
                aa.Add(a[0]);
                a[0] = a[n - 1];
                n--;
                heapSort(a,condition,n,0);
            }
            aa.Add(a[0]);
            for (int i = 0;i < aa.Count;i++) {
                a[i] = aa[i];
            }
        }

        static private void heapSort<T,Q> (List<T> a,Condition<T,Q> condition,int n,int k) where Q:IComparable {//堆排序
            int k1 = 2 * k + 1;//左边
            int k2 = 2 * k + 2;//右边

            if (k1 >= n && k2 >= n)
                return;
            T a1;
            T a2;
            T a3;

            if (k2 < n) {
                a1 = a[k1];
                a2 = a[k2];
                a3 = a[k];
                if (condition(a3).CompareTo(condition(a1)) < 0 && condition(a3).CompareTo(condition(a2)) < 0)
                    return;
                if (condition(a1).CompareTo(condition(a2)) < 0) {
                    swap<T>(a,k1,k);
                    heapSort(a,condition,n,k1);
                } else {
                    swap<T>(a,k2,k);
                    heapSort(a,condition,n,k2);
                }
            } else {
                a1 = a[k1];
                a3 = a[k];
                if (condition(a3).CompareTo(condition(a1)) < 0)
                    return;
                if (condition(a1).CompareTo(condition(a3)) < 0) {
                    swap<T>(a,k1,k);
                    heapSort(a,condition,n,k1);
                }
            }

        }
        static public void Heap_Sort<T,Q> (List<T> a,Condition<T,Q> condition) where Q:IComparable {//堆排序
            for (int i = (a.Count - 1) / 2;i >= 0;i--)
                heapSort<T,Q>(a,condition,a.Count,i);
            int n = a.Count;
            List<T> aa = new List<T>();
            while (a.Count > 0 && condition(a[0]).CompareTo(condition(a[n - 1])) != 0) {
                aa.Add(a[0]);
                a[0] = a[n - 1];
                n--;
                heapSort(a,condition,n,0);
            }
            aa.Add(a[0]);
            for (int i = 0;i < aa.Count;i++) {
                a[i] = aa[i];
            }
        }

        //归并排序
        public static void Merge_Sort<T,Q> (T[] arr,Condition<T,Q> condition) where Q:IComparable {
            T[] temp = new T[arr.Length];//在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间
            mergeSort<T,Q>(arr,condition,0,arr.Length - 1,temp);
        }
        private static void mergeSort<T,Q> (T[] arr,Condition<T,Q> condition,int left,int right,T[] temp) where Q:IComparable {
            if (left < right) {
                int mid = (left + right) / 2;
                mergeSort<T,Q>(arr,condition,left,mid,temp);//左边归并排序,使得左子序列有序
                mergeSort<T,Q>(arr,condition,mid + 1,right,temp);//右边归并排序,使得右子序列有序
                merge<T,Q>(arr,condition,left,mid,right,temp);//将两个有序子数组合并操作
            }
        }
        private static void merge<T,Q> (T[] arr,Condition<T,Q> condition,int left,int mid,int right,T[] temp) where Q:IComparable {
            int i = left;//左序列指针
            int j = mid + 1;//右序列指针
            int t = 0;//临时数组指针
            while (i <= mid && j <= right) {
                if (condition(arr[i]).CompareTo(condition(arr[j])) < 0) {
                    temp[t++] = arr[i++];
                } else {
                    temp[t++] = arr[j++];
                }
            }
            while (i <= mid) {//将左边剩余元素填充进temp中
                temp[t++] = arr[i++];
            }
            while (j <= right) {//将右序列剩余元素填充进temp中
                temp[t++] = arr[j++];
            }
            t = 0;
            //将temp中的元素全部拷贝到原数组中
            while (left <= right) {
                arr[left++] = temp[t++];
            }
        }


        public static void Merge_Sort<T,Q> (List<T> arr,Condition<T,Q> condition) where Q:IComparable {
            T[] temp = new T[arr.Count];//在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间
            mergeSort<T,Q>(arr,condition,0,arr.Count - 1,temp);
        }
        private static void mergeSort<T,Q> (List<T> arr,Condition<T,Q> condition,int left,int right,T[] temp) where Q:IComparable {
            if (left < right) {
                int mid = (left + right) / 2;
                mergeSort<T,Q>(arr,condition,left,mid,temp);//左边归并排序,使得左子序列有序
                mergeSort<T,Q>(arr,condition,mid + 1,right,temp);//右边归并排序,使得右子序列有序
                merge<T,Q>(arr,condition,left,mid,right,temp);//将两个有序子数组合并操作
            }
        }
        private static void merge<T,Q> (List<T> arr,Condition<T,Q> condition,int left,int mid,int right,T[] temp) where Q:IComparable {
            int i = left;//左序列指针
            int j = mid + 1;//右序列指针
            int t = 0;//临时数组指针
            while (i <= mid && j <= right) {
                if (condition(arr[i]).CompareTo(condition(arr[j])) < 0) {
                    temp[t++] = arr[i++];
                } else {
                    temp[t++] = arr[j++];
                }
            }
            while (i <= mid) {//将左边剩余元素填充进temp中
                temp[t++] = arr[i++];
            }
            while (j <= right) {//将右序列剩余元素填充进temp中
                temp[t++] = arr[j++];
            }
            t = 0;
            //将temp中的元素全部拷贝到原数组中
            while (left <= right) {
                arr[left++] = temp[t++];
            }
        }


        static void swap<T> (List<T> a,int n,int m) {
            T t;
            t = a[n];
            a[n] = a[m];
            a[m] = t;
        }
        static void swap<T> (ref T a,ref T b) {
            T t;
            t = b;
            b = a;
            a = t;
        }
    }
}

将他做成一个DLL之后,以后就是我们的人啦,
调用方法:

    struct TextSort {
        public TextSort(string name, int id) {
            this.name = name;
            this.id = id;
        }
        string name;
        int id;
        public int returnID() {
            return id;
        }
    }

    List<TextSort> sortList;
    void Start() {
        sortList = new List<TextSort>();

        sortList.Add(new TextSort("name1", 4));
        sortList.Add(new TextSort("name2", 5));
        sortList.Add(new TextSort("name3", 1));
        sortList.Add(new TextSort("name3", 2));
        Sort.Sort.Insertion_Sort<TextSort, int>(sortList, (a)=>{
            return a.returnID();
        });

        foreach (var sortObject in sortList) {
            Debug.Log(sortObject.returnID());
        }
    }
成果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,198评论 6 514
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,334评论 3 398
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,643评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,495评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,502评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,156评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,743评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,659评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,200评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,282评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,424评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,107评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,789评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,264评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,390评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,798评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,435评论 2 359