文章引用地址:https://www.cnblogs.com/fengyeqingxiang/archive/2019/06/14/11021852.html

C#所有经典排序算法汇总

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
1、选择排序
 
选择排序
class SelectionSorter    
{    
    private int min;    
    public void Sort(int[] arr)    
    {    
        for (int i = 0; i < arr.Length - 1; ++i)    
        {    
            min = i;    
            for (int j = i + 1; j < arr.Length; ++j)    
            {    
                if (arr[j] < arr[min])    
                    min = j;    
            }    
            int t = arr[min];    
            arr[min] = arr[i];    
            arr[i] = t;    
        }    
    }    
}
 
2、冒泡排序
 
冒泡排序
class EbullitionSorter    
{    
    public void Sort(int[] arr)    
    {    
        int i, j, temp;    
        bool done = false;    
        j = 1;    
        while ((j < arr.Length) && (!done))//判断长度    
        {    
            done = true;    
            for (i = 0; i < arr.Length - j; i++)    
            {    
                if (arr[i] > arr[i + 1])    
                {    
                    done = false;    
                    temp = arr[i];    
                    arr[i] = arr[i + 1];//交换数据    
                    arr[i + 1] = temp;    
                }    
            }    
            j++;    
        }    
    }      
}
 
3、快速排序
 
快速排序
class QuickSorter    
{    
    private void swap(ref int l, ref int r)    
    {    
        int temp;    
        temp = l;    
        l = r;    
        r = temp;    
    }    
    public void Sort(int[] list, int low, int high)    
    {    
        int pivot;//存储分支点    
        int l, r;    
        int mid;    
        if (high <= low)    
            return;    
        else if (high == low + 1)    
        {    
            if (list[low] > list[high])    
                swap(ref list[low], ref list[high]);    
            return;    
        }    
        mid = (low + high) >> 1;    
        pivot = list[mid];    
        swap(ref list[low], ref list[mid]);    
        l = low + 1;    
        r = high;    
        do  
        {    
        while (l <= r && list[l] < pivot)    
            l++;    
        while (list[r] >= pivot)    
            r--;    
            if (l < r)    
                swap(ref list[l], ref list[r]);    
        while (l < r);    
        list[low] = list[r];    
        list[r] = pivot;    
        if (low + 1 < r)    
            Sort(list, low, r - 1);    
        if (r + 1 < high)    
            Sort(list, r + 1, high);    
    }      
}    
 
4、插入排序
 
插入排序
public class InsertionSorter    
{    
    public void Sort(int[] arr)    
    {    
        for (int i = 1; i < arr.Length; i++)    
        {    
            int t = arr[i];    
            int j = i;    
            while ((j > 0) && (arr[j - 1] > t))    
            {    
                arr[j] = arr[j - 1];//交换顺序    
                --j;    
            }    
            arr[j] = t;    
        }    
    }    
}    
 
5、希尔排序
 
希尔排序
public class ShellSorter    
{    
    public void Sort(int[] arr)    
    {    
        int inc;    
        for (inc = 1; inc <= arr.Length / 9; inc = 3 * inc + 1) ;    
        for (; inc > 0; inc /= 3)    
        {    
            for (int i = inc + 1; i <= arr.Length; i += inc)    
            {    
                int t = arr[i - 1];    
                int j = i;    
                while ((j > inc) && (arr[j - inc - 1] > t))    
                {    
                    arr[j - 1] = arr[j - inc - 1];//交换数据    
                    j -= inc;    
                }    
                arr[j - 1] = t;    
            }    
        }    
    }  
}  
 
6、归并排序
 
归并排序
        /// <summary>
        /// 归并排序之归:归并排序入口
        /// </summary>
        /// <param name="data">无序的数组</param>
        /// <returns>有序数组</returns>
        /// <author>Lihua(www.zivsoft.com)</author>
        int[] Sort(int[] data)
        {
            //取数组中间下标
            int middle = data.Length / 2;
            //初始化临时数组let,right,并定义result作为最终有序数组
            int[] left = new int[middle], right = new int[middle], result = new int[data.Length];
            if (data.Length % 2 != 0)//若数组元素奇数个,重新初始化右临时数组
            {
                right = new int[middle + 1];
            }
            if (data.Length <= 1)//只剩下1 or 0个元数,返回,不排序
            {
                return data;
            }
            int i = 0, j = 0;
            foreach (int in data)//开始排序
            {
                if (i < middle)//填充左数组
                {
                    left[i] = x;
                    i++;
                }
                else//填充右数组
                {
                    right[j] = x;
                    j++;
                }
            }
            left = Sort(left);//递归左数组
            right = Sort(right);//递归右数组
            result = Merge(left, right);//开始排序
            //this.Write(result);//输出排序,测试用(lihua debug)
            return result;
        }
        /// <summary>
        /// 归并排序之并:排序在这一步
        /// </summary>
        /// <param name="a">左数组</param>
        /// <param name="b">右数组</param>
        /// <returns>合并左右数组排序后返回</returns>
        int[] Merge(int[] a, int[] b)
        {
            //定义结果数组,用来存储最终结果
            int[] result = new int[a.Length + b.Length];
            int i = 0, j = 0, k = 0;
            while (i < a.Length && j < b.Length)
            {
                if (a[i] < b[j])//左数组中元素小于右数组中元素
                {
                    result[k++] = a[i++];//将小的那个放到结果数组
                }
                else//左数组中元素大于右数组中元素
                {
                    result[k++] = b[j++];//将小的那个放到结果数组
                }
            }
            while (i < a.Length)//这里其实是还有左元素,但没有右元素
            {
                result[k++] = a[i++];
            }
            while (j < b.Length)//右右元素,无左元素
            {
                result[k++] = b[j++];
            }
            return result;//返回结果数组
        }
注:此算法由周利华提供(http://www.cnblogs.com/architect/archive/2009/05/06/1450489.html
 
7、基数排序
 
基数排序
        //基数排序
        public int[] RadixSort(int[] ArrayToSort, int digit)
        {  
            //low to high digit
            for (int k = 1; k <= digit; k++)
            {      
                //temp array to store the sort result inside digit
                int[] tmpArray = new int[ArrayToSort.Length];
                //temp array for countingsort
                int[] tmpCountingSortArray = new int[10]{0,0,0,0,0,0,0,0,0,0};        
                //CountingSort        
                for (int i = 0; i < ArrayToSort.Length; i++)        
                {          
                    //split the specified digit from the element
                    int tmpSplitDigit = ArrayToSort[i]/(int)Math.Pow(10,k-1) - (ArrayToSort[i]/(int)Math.Pow(10,k))*10;
                    tmpCountingSortArray[tmpSplitDigit] += 1;
                }        
                for (int m = 1; m < 10; m++)      
                {            
                    tmpCountingSortArray[m] += tmpCountingSortArray[m - 1];        
                }        
                //output the value to result      
                for (int n = ArrayToSort.Length - 1; n >= 0; n--)      
                {          
                    int tmpSplitDigit = ArrayToSort[n] / (int)Math.Pow(10,k - 1) - (ArrayToSort[n]/(int)Math.Pow(10,k)) * 10;          
                    tmpArray[tmpCountingSortArray[tmpSplitDigit]-1] = ArrayToSort[n];            
                    tmpCountingSortArray[tmpSplitDigit] -= 1;      
                }        
                //copy the digit-inside sort result to source array      
                for (int p = 0; p < ArrayToSort.Length; p++)      
                {          
                    ArrayToSort[p] = tmpArray[p];      
                }  
            }    
            return ArrayToSort;
        }
 
8、计数排序
 
计数排序
//计数排序
        /// <summary>
        /// counting sort
        /// </summary>
        /// <param name="arrayA">input array</param>
        /// <param name="arrange">the value arrange in input array</param>
        /// <returns></returns>
        public int[] CountingSort(int[] arrayA, int arrange)
        {    
            //array to store the sorted result,  
            //size is the same with input array.
            int[] arrayResult = new int[arrayA.Length];    
            //array to store the direct value in sorting process  
            //include index 0;    
            //size is arrange+1;    
            int[] arrayTemp = new int[arrange+1];    
            //clear up the temp array    
            for(int i = 0; i <= arrange; i++)    
            {        
                arrayTemp[i] = 0;  
            }    
            //now temp array stores the count of value equal  
            for(int j = 0; j < arrayA.Length; j++)  
            {      
                arrayTemp[arrayA[j]] += 1;  
            }    
            //now temp array stores the count of value lower and equal  
            for(int k = 1; k <= arrange; k++)  
            {      
                arrayTemp[k] += arrayTemp[k - 1];  
            }    
            //output the value to result    
            for (int m = arrayA.Length-1; m >= 0; m--)  
            {        
                arrayResult[arrayTemp[arrayA[m]] - 1] = arrayA[m];    
                arrayTemp[arrayA[m]] -= 1;  
            }    
            return arrayResult;
        }
 
 
9、小根堆排序
 
小根堆排序
/// <summary>
        /// 小根堆排序
        /// </summary>
        /// <param name="dblArray"></param>
        /// <param name="StartIndex"></param>
        /// <returns></returns>
 
        private void HeapSort(ref double[] dblArray)
        {
            for (int i = dblArray.Length - 1; i >= 0; i--)
            {
                if (2 * i + 1 < dblArray.Length)
                {
                    int MinChildrenIndex = 2 * i + 1;
                    //比较左子树和右子树,记录最小值的Index
                    if (2 * i + 2 < dblArray.Length)
                    {
                        if (dblArray[2 * i + 1] > dblArray[2 * i + 2])
                            MinChildrenIndex = 2 * i + 2;
                    }
                    if (dblArray[i] > dblArray[MinChildrenIndex])
                    {
 
 
                        ExchageValue(ref dblArray[i], ref dblArray[MinChildrenIndex]);
                        NodeSort(ref dblArray, MinChildrenIndex);
                    }
                }
            }
        }
 
        /// <summary>
        /// 节点排序
        /// </summary>
        /// <param name="dblArray"></param>
        /// <param name="StartIndex"></param>
 
        private void NodeSort(ref double[] dblArray, int StartIndex)
        {
            while (2 * StartIndex + 1 < dblArray.Length)
            {
                int MinChildrenIndex = 2 * StartIndex + 1;
                if (2 * StartIndex + 2 < dblArray.Length)
                {
                    if (dblArray[2 * StartIndex + 1] > dblArray[2 * StartIndex + 2])
                    {
                        MinChildrenIndex = 2 * StartIndex + 2;
                    }
                }
                if (dblArray[StartIndex] > dblArray[MinChildrenIndex])
                {
                    ExchageValue(ref dblArray[StartIndex], ref dblArray[MinChildrenIndex]);
                    StartIndex = MinChildrenIndex;
                }
            }
        }
 
        /// <summary>
        /// 交换值
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        private void ExchageValue(ref double A, ref double B)
        {
            double Temp = A;
            A = B;
            B = Temp;
        }

C# 常用排序算法的更多相关文章

  1. Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法

    Java 常用排序算法/程序员必须掌握的 8大排序算法 本文由网络资料整理转载而来,如有问题,欢迎指正! 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排 ...

  2. 转载部长一篇大作:常用排序算法之JavaScript实现

    转载部长一篇大作:常用排序算法之JavaScript实现 注:本文是转载实验室同门王部长的大作,找实习找工作在即,本文颇有用处!原文出处:http://www.cnblogs.com/ywang172 ...

  3. Java 常用排序算法/程序员必须掌握的 8大排序算法

    Java 常用排序算法/程序员必须掌握的 8大排序算法 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分配 ...

  4. 常用排序算法的python实现和性能分析

    常用排序算法的python实现和性能分析 一年一度的换工作高峰又到了,HR大概每天都塞几份简历过来,基本上一天安排两个面试的话,当天就只能加班干活了.趁着面试别人的机会,自己也把一些基础算法和一些面试 ...

  5. 面试中常用排序算法实现(Java)

    当我们进行数据处理的时候,往往需要对数据进行查找操作,一个有序的数据集往往能够在高效的查找算法下快速得到结果.所以排序的效率就会显的十分重要,本篇我们将着重的介绍几个常见的排序算法,涉及如下内容: 排 ...

  6. 常用排序算法java实现

    写在前面:纸上得来终觉浅.基本排序算法的思想,可能很多人都说的头头是到,但能说和能写出来,真的还是有很大区别的. 今天整理了一下各种常用排序算法,当然还不全,后面会继续补充.代码中可能有累赘或错误的地 ...

  7. 我们一起来排序——使用Java语言优雅地实现常用排序算法

    破阵子·春景 燕子来时新社,梨花落后清明. 池上碧苔三四点,叶底黄鹂一两声.日长飞絮轻. 巧笑同桌伙伴,上学径里逢迎. 疑怪昨宵春梦好,元是今朝Offer拿.笑从双脸生. 排序算法--最基础的算法,互 ...

  8. Python实现常用排序算法

    Python实现常用排序算法 冒泡排序 思路: 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完 ...

  9. 第四百一十五节,python常用排序算法学习

    第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...

  10. Java常用排序算法及性能测试集合

    测试报告: Array length: 20000 bubbleSort : 573 ms bubbleSortAdvanced : 596 ms bubbleSortAdvanced2 : 583 ...

随机推荐

  1. 数据库索引的优化及SQL处理过程

    想要设计出好的索引,首先必须了解SQL语句在数据库服务器中的处理过程,本文介绍数据库索引设计与优化中几个对索引优化非常重要的概念. 谓词 谓词就是条件表达式. SQL语句的where子句由一个或者多个 ...

  2. 从零开始的vue学习笔记(一)

    前言 项目要用vue.js,今天开始自学vue.js官方教程,记录下自己的学习摘要,方便后面查阅(此笔记按照学习天数,每天一篇) Vue.js是什么 Vue是一套用于构建用户界面的渐进式框架,Vue ...

  3. python基础(36):pymysql模块

    1. pymysql模块 在使用pymysql模块前需要学习数据库MySQL:<MySQL基础>. 1.1 pymysql的下载和使用 看完MySQL基础,我们都是通过MySQL自带的命令 ...

  4. Burp Suite渗透操作指南 【暴力破解】

    1.1 Intruder高效暴力破解 其实更喜欢称Intruder爆破为Fuzzing.Intruder支持多种爆破模式.分别是:单一字典爆破.多字段相同字典爆破.多字典意义对应爆破.聚合式爆破.最常 ...

  5. vue.js 本地解决跨域

    1.config/index.js下添加proxyTable dev: { // Paths assetsSubDirectory: 'static', assetsPublicPath: '/', ...

  6. LeetCode刷题191122

    博主渣渣一枚,刷刷leetcode给自己瞅瞅,大神们由更好方法还望不吝赐教.题目及解法来自于力扣(LeetCode),传送门. 算法: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. ...

  7. 任正非讲话稿 PDF整理版

    任正非讲话稿 PDF整理版 任正非思想之路 这里收录了任正非讲话稿400余篇,从1994年到2018年,从深圳.中国到东南亚.非洲.欧洲.美洲,从研发.市场.服务到财经.人力资源.战略.内控与公共关系 ...

  8. 史上最全Oracle数据泵常用命令

    本文转自https://blog.csdn.net/Enmotech/article/details/102848825 墨墨导读:expdp和impdp是oracle数据库之间移动数据的工具,本文简 ...

  9. 剑指Offer-38.平衡二叉树(C++/Java)

    题目: 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 分析: 可以从根节点开始遍历每一个节点,求得节点左右子树的最大高度,判断是不是平衡二叉树.这样做的问题在于会重复遍历节点,造成不必要的浪费. 所 ...

  10. HashMap、HashTable 和 ConcurrentHashMap 线程安全问题

    一.HashMap HashMap 是线程不安全的. JDK 1.7 HashMap 采用数组 + 链表的数据结构,多线程背景下,在数组扩容的时候,存在 Entry 链死循环和数据丢失问题. JDK ...