8种主要排序算法的C#实现
Excerpt
8种主要排序算法的实现及优化,包含选择排序,冒泡排序,插入排序,快速排序,归并排序,堆排序,希尔排序,基数排序。文末实际测试并比较。
新的一年到了,很多园友都辞职要去追求更好的工作环境,我也是其中一个,呵呵!
最近闲暇的时候我开始重温一些常用的算法。老早就买了《算法导论》,一直都没啃下去。
这本书确实很好,只是太难读了,总是读了几章就又读不下去了!工作上也几乎用不到。
我这段时间发现看这些排序算法比以前容易了很多,就借此机会将它们整理总结起来。
一是方便以后重温,二是可以应对笔试面试。同时也希望这篇博文可以帮助各位刚辞职和正在学习排序算法的园友。
PS:有可能实现的代码并不是最优的,如果有什么错误或者值得改进的地方,还请大家帮忙指出。
简介
排序算法是我们编程中遇到的最多的算法。目前主流的算法有8种。
平均时间复杂度从高到低依次是:
冒泡排序(o(n2)),选择排序(o(n2)),插入排序(o(n2)),堆排序(o(nlogn)),
归并排序(o(nlogn)),快速排序(o(nlogn)), 希尔排序(o(n1.25)),基数排序(o(n))
这些平均时间复杂度是参照维基百科排序算法罗列的。
是计算的理论平均值,并不意味着你的代码实现能达到这样的程度。
例如希尔排序,时间复杂度是由选择的步长决定的。基数排序时间复杂度最小,
但我实现的基数排序的速度并不是最快的,后面的结果测试图可以看到。
本文代码实现使用的数据源类型为IList,这样可以兼容int[]和List(虽然int[]有ToList(),
List有ToArray(),哈哈!)。
选择排序
选择排序是我觉得最简单暴力的排序方式了。
以前刚接触排序算法的时候,感觉算法太多搞不清,唯独记得选择排序的做法及实现。
原理:找出参与排序的数组最大值,放到末尾(或找到最小值放到开头) 维基入口
实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> SelectSort(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span>for</span> (<span>int</span> i = <span>0</span>; i < data.Count - <span>1</span>; i++<span>) </span><span> 4</span> <span> { </span><span> 5</span> <span>int</span> min =<span> i; </span><span> 6</span> <span>int</span> temp =<span> data[i]; </span><span> 7</span> <span>for</span> (<span>int</span> j = i + <span>1</span>; j < data.Count; j++<span>) </span><span> 8</span> <span> { </span><span> 9</span> <span>if</span> (data[j] <<span> temp) </span><span>10</span> <span> { </span><span>11</span> min =<span> j; </span><span>12</span> temp =<span> data[j]; </span><span>13</span> <span> } </span><span>14</span> <span> } </span><span>15</span> <span>if</span> (min !=<span> i) </span><span>16</span> <span> Swap(data, min, i); </span><span>17</span> <span> } </span><span>18</span> }
|
过程解析:将剩余数组的最小数交换到开头。
冒泡排序
冒泡排序是笔试面试经常考的内容,虽然它是这些算法里排序速度最慢的(汗),后面有测试为证。
原理:从头开始,每一个元素和它的下一个元素比较,如果它大,就将它与比较的元素交换,否则不动。
这意味着,大的元素总是在向后慢慢移动直到遇到比它更大的元素。所以每一轮交换完成都能将最大值
冒到最后。 维基入口
实现如下:
1 2 3 4 5 6 7 8 9 10 11
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> BubbleSort(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span>for</span> (<span>int</span> i = data.Count - <span>1</span>; i > <span>0</span>; i--<span>) </span><span> 4</span> <span> { </span><span> 5</span> <span>for</span> (<span>int</span> j = <span>0</span>; j < i; j++<span>) </span><span> 6</span> <span> { </span><span> 7</span> <span>if</span> (data[j] > data[j + <span>1</span><span>]) </span><span> 8</span> Swap(data, j, j + <span>1</span><span>); </span><span> 9</span> <span> } </span><span>10</span> <span> } </span><span>11</span> }
|
过程解析:中需要注意的是j<i,每轮冒完泡必然会将最大值排到数组末尾,所以需要排序的数应该是在减少的。
很多网上版本每轮冒完泡后依然还是将所有的数进行第二轮冒泡即j<data.Count-1,这样会增加比较次数。
通过标识提升冒泡排序
在维基上看到,可以通过添加标识来分辨剩余的数是否已经有序来减少比较次数。感觉很有意思,可以试试。
实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> BubbleSortImprovedWithFlag(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span>bool</span><span> flag; </span><span> 4</span> <span>for</span> (<span>int</span> i = data.Count - <span>1</span>; i > <span>0</span>; i--<span>) </span><span> 5</span> <span> { </span><span> 6</span> flag = <span>true</span><span>; </span><span> 7</span> <span>for</span> (<span>int</span> j = <span>0</span>; j < i; j++<span>) </span><span> 8</span> <span> { </span><span> 9</span> <span>if</span> (data[j] > data[j + <span>1</span><span>]) </span><span>10</span> <span> { </span><span>11</span> Swap(data, j, j + <span>1</span><span>); </span><span>12</span> flag = <span>false</span><span>; </span><span>13</span> <span> } </span><span>14</span> <span> } </span><span>15</span> <span>if</span> (flag) <span>break</span><span>; </span><span>16</span> <span> } </span><span>17</span> }
|
过程解析:发现某轮冒泡没有任何数进行交换(即已经有序),就跳出排序。
我起初也以为这个方法是应该有不错效果的,可是实际测试结果并不如想的那样。和未优化耗费时间一样(对于随机数列)。
由果推因,那么应该是冒泡排序对于随机数列,当剩余数列有序的时候,也没几个数要排列了!?
不过如果已经是有序数列或者部分有序的话,这个冒泡方法将会提升很大速度。
鸡尾酒排序(来回排序)
对冒泡排序进行更大的优化
冒泡排序只是单向冒泡,而鸡尾酒来回反复双向冒泡。
原理:自左向右将大数冒到末尾,然后将剩余数列再自右向左将小数冒到开头,如此循环往复。维基入口
实现如下:
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
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> BubbleCocktailSort(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span>bool</span><span> flag; </span><span> 4</span> <span>int</span> m = <span>0</span>, n = <span>0</span><span>; </span><span> 5</span> <span>for</span> (<span>int</span> i = data.Count - <span>1</span>; i > <span>0</span>; i--<span>) </span><span> 6</span> <span> { </span><span> 7</span> flag = <span>true</span><span>; </span><span> 8</span> <span>if</span> (i % <span>2</span> == <span>0</span><span>) </span><span> 9</span> <span> { </span><span>10</span> <span>for</span> (<span>int</span> j = n; j < data.Count - <span>1</span> - m; j++<span>) </span><span>11</span> <span> { </span><span>12</span> <span>if</span> (data[j] > data[j + <span>1</span><span>]) </span><span>13</span> <span> { </span><span>14</span> Swap(data, j, j + <span>1</span><span>); </span><span>15</span> flag = <span>false</span><span>; </span><span>16</span> <span> } </span><span>17</span> <span> } </span><span>18</span> <span>if</span> (flag) <span>break</span><span>; </span><span>19</span> m++<span>; </span><span>20</span> <span> } </span><span>21</span> <span>else</span> <span>22</span> <span> { </span><span>23</span> <span>for</span> (<span>int</span> k = data.Count - <span>1</span> - m; k > n; k--<span>) </span><span>24</span> <span> { </span><span>25</span> <span>if</span> (data[k] < data[k - <span>1</span><span>]) </span><span>26</span> <span> { </span><span>27</span> Swap(data, k, k - <span>1</span><span>); </span><span>28</span> flag = <span>false</span><span>; </span><span>29</span> <span> } </span><span>30</span> <span> } </span><span>31</span> <span>if</span> (flag) <span>break</span><span>; </span><span>32</span> n++<span>; </span><span>33</span> <span> } </span><span>34</span> <span> } </span><span>35</span> }
|
过程解析:分析第i轮冒泡,i是偶数则将剩余数列最大值向右冒泡至末尾,是奇数则将剩余数列最小值
向左冒泡至开头。对于剩余数列,n为始,data.Count-1-m为末。
来回冒泡比单向冒泡:对于随机数列,更容易得到有序的剩余数列。因此这里使用标识将会提升的更加明显。
插入排序
插入排序是一种对于有序数列高效的排序。非常聪明的排序。只是对于随机数列,效率一般,交换的频率高。
原理:通过构建有序数列,将未排序的数从后向前比较,找到合适位置并插入。维基入口
第一个数当作有序数列。
实现如下:
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
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> InsertSort(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span>int</span><span> temp; </span><span> 4</span> <span>for</span> (<span>int</span> i = <span>1</span>; i < data.Count; i++<span>) </span><span> 5</span> <span> { </span><span> 6</span> temp =<span> data[i]; </span><span> 7</span> <span>for</span> (<span>int</span> j = i - <span>1</span>; j >= <span>0</span>; j--<span>) </span><span> 8</span> <span> { </span><span> 9</span> <span>if</span> (data[j] ><span> temp) </span><span>10</span> <span> { </span><span>11</span> data[j + <span>1</span>] =<span> data[j]; </span><span>12</span> <span>if</span> (j == <span>0</span><span>) </span><span>13</span> <span> { </span><span>14</span> data[<span>0</span>] =<span> temp; </span><span>15</span> <span>break</span><span>; </span><span>16</span> <span> } </span><span>17</span> <span> } </span><span>18</span> <span>else</span> <span>19</span> <span> { </span><span>20</span> data[j + <span>1</span>] =<span> temp; </span><span>21</span> <span>break</span><span>; </span><span>22</span> <span> } </span><span>23</span> <span> } </span><span>24</span> <span> } </span><span>25</span> }
|
过程解析:将要排序的数(索引为i)存储起来,向前查找合适位置j+1,将i-1到j+1的元素依次向后
移动一位,空出j+1,然后将之前存储的值放在这个位置。
这个方法写的不如维基上的简洁清晰,由于合适位置是j+1所以多出了对j==0的判断,但实际效率影响无差别。
建议比照维基和我写的排序,自行选择。
二分查找法优化插入排序
插入排序主要工作是在有序的数列中对要排序的数查找合适的位置,而查找里面经典的二分查找法正可以适用。
原理:通过二分查找法的方式找到一个位置索引。当要排序的数插入这个位置时,大于前一个数,小于后一个数。
实现如下:
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
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> InsertSortImprovedWithBinarySearch(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span>int</span><span> temp; </span><span> 4</span> <span>int</span><span> tempIndex; </span><span> 5</span> <span>for</span> (<span>int</span> i = <span>1</span>; i < data.Count; i++<span>) </span><span> 6</span> <span> { </span><span> 7</span> temp =<span> data[i]; </span><span> 8</span> tempIndex = BinarySearchForInsertSort(data, <span>0</span><span>, i, i); </span><span> 9</span> <span>for</span> (<span>int</span> j = i - <span>1</span>; j >= tempIndex; j--<span>) </span><span>10</span> <span> { </span><span>11</span> data[j + <span>1</span>] =<span> data[j]; </span><span>12</span> <span> } </span><span>13</span> data[tempIndex] =<span> temp; </span><span>14</span> <span> } </span><span>15</span> <span> } </span><span>16</span> <span>17</span> <span>public</span> <span>static</span> <span>int</span> BinarySearchForInsertSort(IList<<span>int</span>> data, <span>int</span> low, <span>int</span> high, <span>int</span><span> key) </span><span>18</span> <span> { </span><span>19</span> <span>if</span> (low >= data.Count - <span>1</span><span>) </span><span>20</span> <span>return</span> data.Count - <span>1</span><span>; </span><span>21</span> <span>if</span> (high <= <span>0</span><span>) </span><span>22</span> <span>return</span> <span>0</span><span>; </span><span>23</span> <span>int</span> mid = (low + high) / <span>2</span><span>; </span><span>24</span> <span>if</span> (mid == key) <span>return</span><span> mid; </span><span>25</span> <span>if</span> (data[key] ><span> data[mid]) </span><span>26</span> <span> { </span><span>27</span> <span>if</span> (data[key] < data[mid + <span>1</span><span>]) </span><span>28</span> <span>return</span> mid + <span>1</span><span>; </span><span>29</span> <span>return</span> BinarySearchForInsertSort(data, mid + <span>1</span><span>, high, key); </span><span>30</span> <span> } </span><span>31</span> <span>else <span>// data[key] <= data[mid]</span></span> <span>32</span> <span> { </span><span>33</span> <span>if</span> (mid - <span>1</span> < <span>0</span>) <span>return</span> <span>0</span><span>; </span><span>34</span> <span>if</span> (data[key] > data[mid - <span>1</span><span>]) </span><span>35</span> <span>return</span><span> mid; </span><span>36</span> <span>return</span> BinarySearchForInsertSort(data, low, mid - <span>1</span><span>, key); </span><span>37</span> <span> } </span><span>38</span> }
|
过程解析:需要注意的是二分查找方法实现中high-low==1的时候mid==low,所以需要33行
mid-1<0即mid==0的判断,否则下行会索引越界。
快速排序
快速排序是一种有效比较较多的高效排序。它包含了“分而治之”以及“哨兵”的思想。
原理:从数列中挑选一个数作为“哨兵”,使比它小的放在它的左侧,比它大的放在它的右侧。将要排序是数列递归地分割到
最小数列,每次都让分割出的数列符合“哨兵”的规则,自然就将数列变得有序。 维基入口
实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> QuickSortStrict(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> QuickSortStrict(data, <span>0</span>, data.Count - <span>1</span><span>); </span><span> 4</span> <span> } </span><span> 5</span> <span> 6</span> <span>public</span> <span>static</span> <span>void</span> QuickSortStrict(IList<<span>int</span>> data, <span>int</span> low, <span>int</span><span> high) </span><span> 7</span> <span> { </span><span> 8</span> <span>if</span> (low >= high) <span>return</span><span>; </span><span> 9</span> <span>int</span> temp =<span> data[low]; </span><span>10</span> <span>int</span> i = low + <span>1</span>, j =<span> high; </span><span>11</span> <span>while</span> (<span>true</span><span>) </span><span>12</span> <span> { </span><span>13</span> <span>while</span> (data[j] > temp) j--<span>; </span><span>14</span> <span>while</span> (data[i] < temp && i < j) i++<span>; </span><span>15</span> <span>if</span> (i >= j) <span>break</span><span>; </span><span>16</span> <span> Swap(data, i, j); </span><span>17</span> i++; j--<span>; </span><span>18</span> <span> } </span><span>19</span> <span>if</span> (j !=<span> low) </span><span>20</span> <span> Swap(data, low, j); </span><span>21</span> QuickSortStrict(data, j + <span>1</span><span>, high); </span><span>22</span> QuickSortStrict(data, low, j - <span>1</span><span>); </span><span>23</span> }
|
过程解析:取的哨兵是数列的第一个值,然后从第二个和末尾同时查找,左侧要显示的是小于哨兵的值,
所以要找到不小于的i,右侧要显示的是大于哨兵的值,所以要找到不大于的j。将找到的i和j的数交换,
这样可以减少交换次数。i>=j时,数列全部查找了一遍,而不符合条件j必然是在小的那一边,而哨兵
是第一个数,位置本应是小于自己的数。所以将哨兵与j交换,使符合“哨兵”的规则。
这个版本的缺点在于如果是有序数列排序的话,递归次数会很可怕的。
另一个版本
这是维基上的一个C#版本,我觉得很有意思。这个版本并没有严格符合“哨兵”的规则。但却将“分而治之”
以及“哨兵”思想融入其中,代码简洁。
实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> QuickSortRelax(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> QuickSortRelax(data, <span>0</span>, data.Count - <span>1</span><span>); </span><span> 4</span> <span> } </span><span> 5</span> <span> 6</span> <span>public</span> <span>static</span> <span>void</span> QuickSortRelax(IList<<span>int</span>> data, <span>int</span> low, <span>int</span><span> high) </span><span> 7</span> <span> { </span><span> 8</span> <span>if</span> (low >= high) <span>return</span><span>; </span><span> 9</span> <span>int</span> temp = data[(low + high) / <span>2</span><span>]; </span><span>10</span> <span>int</span> i = low - <span>1</span>, j = high + <span>1</span><span>; </span><span>11</span> <span>while</span> (<span>true</span><span>) </span><span>12</span> <span> { </span><span>13</span> <span>while</span> (data[++i] <<span> temp) ; </span><span>14</span> <span>while</span> (data[--j] ><span> temp) ; </span><span>15</span> <span>if</span> (i >= j) <span>break</span><span>; </span><span>16</span> <span> Swap(data, i, j); </span><span>17</span> <span> } </span><span>18</span> QuickSortRelax(data, j + <span>1</span><span>, high); </span><span>19</span> QuickSortRelax(data, low, i - <span>1</span><span>); </span><span>20</span> }
|
过程解析:取的哨兵是数列中间的数。将数列分成两波,左侧小于等于哨兵,右侧大于等于哨兵。
也就是说,哨兵不一定处于两波数的中间。虽然哨兵不在中间,但不妨碍“哨兵”的思想的实现。所以
这个实现也可以达到快速排序的效果。但却造成了每次递归完成,要排序的数列数总和没有减少(除非i==j)。
针对这个版本的缺点,我进行了优化
实现如下:
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
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> QuickSortRelaxImproved(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> QuickSortRelaxImproved(data, <span>0</span>, data.Count - <span>1</span><span>); </span><span> 4</span> <span> } </span><span> 5</span> <span> 6</span> <span>public</span> <span>static</span> <span>void</span> QuickSortRelaxImproved(IList<<span>int</span>> data, <span>int</span> low, <span>int</span><span> high) </span><span> 7</span> <span> { </span><span> 8</span> <span>if</span> (low >= high) <span>return</span><span>; </span><span> 9</span> <span>int</span> temp = data[(low + high) / <span>2</span><span>]; </span><span>10</span> <span>int</span> i = low - <span>1</span>, j = high + <span>1</span><span>; </span><span>11</span> <span>int</span> index = (low + high) / <span>2</span><span>; </span><span>12</span> <span>while</span> (<span>true</span><span>) </span><span>13</span> <span> { </span><span>14</span> <span>while</span> (data[++i] <<span> temp) ; </span><span>15</span> <span>while</span> (data[--j] ><span> temp) ; </span><span>16</span> <span>if</span> (i >= j) <span>break</span><span>; </span><span>17</span> <span> Swap(data, i, j); </span><span>18</span> <span>if</span> (i == index) index =<span> j; </span><span>19</span> <span>else</span> <span>if</span> (j == index) index =<span> i; </span><span>20</span> <span> } </span><span>21</span> <span>if</span> (j ==<span> i) </span><span>22</span> <span> { </span><span>23</span> QuickSortRelaxImproved(data, j + <span>1</span><span>, high); </span><span>24</span> QuickSortRelaxImproved(data, low, i - <span>1</span><span>); </span><span>25</span> <span> } </span><span>26</span> <span>else</span> <span>//</span><span>i-j==1</span> <span>27</span> <span> { </span><span>28</span> <span>if</span> (index >=<span> i) </span><span>29</span> <span> { </span><span>30</span> <span>if</span> (index !=<span> i) </span><span>31</span> <span> Swap(data, index, i); </span><span>32</span> QuickSortRelaxImproved(data, i + <span>1</span><span>, high); </span><span>33</span> QuickSortRelaxImproved(data, low, i - <span>1</span><span>); </span><span>34</span> <span> } </span><span>35</span> <span>else <span>//</span><span>index < i</span></span> <span>36</span> <span> { </span><span>37</span> <span>if</span> (index !=<span> j) </span><span>38</span> <span> Swap(data, index, j); </span><span>39</span> QuickSortRelaxImproved(data, j + <span>1</span><span>, high); </span><span>40</span> QuickSortRelaxImproved(data, low, j - <span>1</span><span>); </span><span>41</span> <span> } </span><span>42</span> <span> } </span><span>43</span> }
|
过程解析:定义了一个变量Index,来跟踪哨兵的位置。发现哨兵最后在小于自己的那堆,
那就与j交换,否则与i交换。达到每次递归都能减少要排序的数列数总和的目的。
归并排序
归并排序也是采用“分而治之”的方式。刚发现分治法是一种算法范式,我还一直以为是一种需要意会的思想呢。
不好意思了,孤陋寡闻了,哈哈!
原理:将两个有序的数列,通过比较,合并为一个有序数列。 维基入口
为方便理解,此处实现用了List的一些方法,随后有IList版本。
实现如下:
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
| <span> 1</span> <span>public</span> <span>static</span> List<<span>int</span>> MergeSortOnlyList(List<<span>int</span>> data, <span>int</span> low, <span>int</span><span> high) </span><span> 2</span> <span> { </span><span> 3</span> <span>if</span> (low ==<span> high) </span><span> 4</span> <span>return</span> <span>new</span> List<<span>int</span>><span> { data[low] }; </span><span> 5</span> List<<span>int</span>> mergeData = <span>new</span> List<<span>int</span>><span>(); </span><span> 6</span> <span>int</span> mid = (low + high) / <span>2</span><span>; </span><span> 7</span> List<<span>int</span>> leftData =<span> MergeSortOnlyList(data, low, mid); </span><span> 8</span> List<<span>int</span>> rightData = MergeSortOnlyList(data, mid + <span>1</span><span>, high); </span><span> 9</span> <span>int</span> i = <span>0</span>, j = <span>0</span><span>; </span><span>10</span> <span>while</span> (<span>true</span><span>) </span><span>11</span> <span> { </span><span>12</span> <span>if</span> (leftData[i] <<span> rightData[j]) </span><span>13</span> <span> { </span><span>14</span> <span> mergeData.Add(leftData[i]); </span><span>15</span> <span>if</span> (++i ==<span> leftData.Count) </span><span>16</span> <span> { </span><span>17</span> mergeData.AddRange(rightData.GetRange(j, rightData.Count -<span> j)); </span><span>18</span> <span>break</span><span>; </span><span>19</span> <span> } </span><span>20</span> <span> } </span><span>21</span> <span>else</span> <span>22</span> <span> { </span><span>23</span> <span> mergeData.Add(rightData[j]); </span><span>24</span> <span>if</span> (++j ==<span> rightData.Count) </span><span>25</span> <span> { </span><span>26</span> mergeData.AddRange(leftData.GetRange(i, leftData.Count -<span> i)); </span><span>27</span> <span>break</span><span>; </span><span>28</span> <span> } </span><span>29</span> <span> } </span><span>30</span> <span> } </span><span>31</span> <span>return</span><span> mergeData; </span><span>32</span> <span> } </span><span>33</span> <span>34</span> <span>public</span> <span>static</span> List<<span>int</span>> MergeSortOnlyList(List<<span>int</span>><span> data) </span><span>35</span> <span> { </span><span>36</span> data = MergeSortOnlyList(data, <span>0</span>, data.Count - <span>1</span><span>); <span>//不会改变外部引用 参照<a href="http://www.cnblogs.com/fatbird/p/parametersInCsharp.html" target="_blank">C#参数传递 </a></span></span><span>37</span> <span>return</span><span> data; </span><span>38</span> }
|
过程解析:将数列分为两部分,分别得到两部分数列的有序版本,然后逐个比较,将比较出的小数逐个放进
新的空数列中。当一个数列放完后,将另一个数列剩余数全部放进去。
IList版本
实现如下:
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
| <span> 1</span> <span>public</span> <span>static</span> IList<<span>int</span>> MergeSort(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> data = MergeSort(data, <span>0</span>, data.Count - <span>1</span><span>); </span><span> 4</span> <span>return</span><span> data; </span><span> 5</span> <span> } </span><span> 6</span> <span> 7</span> <span>public</span> <span>static</span> IList<<span>int</span>> MergeSort(IList<<span>int</span>> data, <span>int</span> low, <span>int</span><span> high) </span><span> 8</span> <span> { </span><span> 9</span> <span>int</span> length = high - low + <span>1</span><span>; </span><span>10</span> IList<<span>int</span>> mergeData =<span> NewInstance(data, length); </span><span>11</span> <span>if</span> (low ==<span> high) </span><span>12</span> <span> { </span><span>13</span> mergeData[<span>0</span>] =<span> data[low]; </span><span>14</span> <span>return</span><span> mergeData; </span><span>15</span> <span> } </span><span>16</span> <span>int</span> mid = (low + high) / <span>2</span><span>; </span><span>17</span> IList<<span>int</span>> leftData =<span> MergeSort(data, low, mid); </span><span>18</span> IList<<span>int</span>> rightData = MergeSort(data, mid + <span>1</span><span>, high); </span><span>19</span> <span>int</span> i = <span>0</span>, j = <span>0</span><span>; </span><span>20</span> <span>while</span> (<span>true</span><span>) </span><span>21</span> <span> { </span><span>22</span> <span>if</span> (leftData[i] <<span> rightData[j]) </span><span>23</span> <span> { </span><span>24</span> mergeData[i + j] = leftData[i++]; <span>//</span><span>不能使用Add,Array Length不可变</span> <span>25</span> <span>if</span> (i ==<span> leftData.Count) </span><span>26</span> <span> { </span><span>27</span> <span>int</span> rightLeft = rightData.Count -<span> j; </span><span>28</span> <span>for</span> (<span>int</span> m = <span>0</span>; m < rightLeft; m++<span>) </span><span>29</span> <span> { </span><span>30</span> mergeData[i + j] = rightData[j++<span>]; </span><span>31</span> <span> } </span><span>32</span> <span>break</span><span>; </span><span>33</span> <span> } </span><span>34</span> <span> } </span><span>35</span> <span>else</span> <span>36</span> <span> { </span><span>37</span> mergeData[i + j] = rightData[j++<span>]; </span><span>38</span> <span>if</span> (j ==<span> rightData.Count) </span><span>39</span> <span> { </span><span>40</span> <span>int</span> leftleft = leftData.Count -<span> i; </span><span>41</span> <span>for</span> (<span>int</span> n = <span>0</span>; n < leftleft; n++<span>) </span><span>42</span> <span> { </span><span>43</span> mergeData[i + j] = leftData[i++<span>]; </span><span>44</span> <span> } </span><span>45</span> <span>break</span><span>; </span><span>46</span> <span> } </span><span>47</span> <span> } </span><span>48</span> <span> } </span><span>49</span> <span>return</span><span> mergeData; </span><span>50</span> <span>51</span> }
|
过程原理与上个一样,此处就不赘述了。
堆排序
堆排序是根据堆这种数据结构设计的一种算法。堆的特性:父节点的值总是小于(或大于)它的子节点。近似二叉树。
原理:将数列构建为最大堆数列(即父节点总是最大值),将最大值(即根节点)交换到数列末尾。这样要排序的数列数总和减少,
同时根节点不再是最大值,调整最大堆数列。如此重复,最后得到有序数列。 维基入口 有趣的演示
实现准备:如何将数列构造为堆——父节点i的左子节点为2i+1,右子节点为2i+2。节点i的父节点为floor((i-1)/2)。
实现如下(这个实现判断和临时变量使用太多,导致效率低,评论中@小城故事提出了更好的实现):
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
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> HeapSort(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span> BuildMaxHeapify(data); </span><span> 4</span> <span>int</span> j =<span> data.Count; </span><span> 5</span> <span>for</span> (<span>int</span> i = <span>0</span>; i <<span> j; ) </span><span> 6</span> <span> { </span><span> 7</span> Swap(data, i, --<span>j); </span><span> 8</span> <span>if</span> (j - <span>2</span> < <span>0</span><span>) <span>//只剩下1个数 j代表余下要排列的数的个数 </span></span><span> 9</span> <span>break</span><span>; </span><span>10</span> <span>int</span> k = <span>0</span><span>; </span><span>11</span> <span>while</span> (<span>true</span><span>) </span><span>12</span> <span> { </span><span>13</span> <span>if</span> (k > (j - <span>2</span>) / <span>2</span>) <span>break</span><span>; <span>//即:k > ((j-1)-1)/2</span> <span>超出最后一个父节点的位置 </span></span><span>14</span> <span>else</span> <span>15</span> <span> { </span><span>16</span> <span>int</span> temp =<span> k; </span><span>17</span> k = ReSortMaxBranch(data, k, <span>2</span> * k + <span>1</span>, <span>2</span> * k + <span>2</span>, j - <span>1</span><span>); </span><span>18</span> <span>if</span> (temp == k) <span>break</span><span>; </span><span>19</span> <span> } </span><span>20</span> <span> } </span><span>21</span> <span> } </span><span>22</span> <span> } </span><span>23</span> <span>24</span> <span>public</span> <span>static</span> <span>void</span> BuildMaxHeapify(IList<<span>int</span>><span> data) </span><span>25</span> <span> { </span><span>26</span> <span>for</span> (<span>int</span> i = data.Count / <span>2</span> - <span>1</span>; i >= <span>0</span>; i--<span>) <span>//(data.Count-1)-1)/2为数列最大父节点索引 </span></span><span>27</span> <span> { </span><span>28</span> <span>int</span> temp =<span> i; </span><span>29</span> temp = ReSortMaxBranch(data, i, <span>2</span> * i + <span>1</span>, <span>2</span> * i + <span>2</span>, data.Count - <span>1</span><span>); </span><span>30</span> <span>if</span> (temp !=<span> i) </span><span>31</span> <span> { </span><span>32</span> <span>int</span> k =<span> i; </span><span>33</span> <span>while</span> (k != temp && temp <= data.Count / <span>2</span> - <span>1</span><span>) </span><span>34</span> <span> { </span><span>35</span> k =<span> temp; </span><span>36</span> temp = ReSortMaxBranch(data, temp, <span>2</span> * temp + <span>1</span>, <span>2</span> * temp + <span>2</span>, data.Count - <span>1</span><span>); </span><span>37</span> <span> } </span><span>38</span> <span> } </span><span>39</span> <span> } </span><span>40</span> <span> } </span><span>41</span> <span>42</span> <span>public</span> <span>static</span> <span>int</span> ReSortMaxBranch(IList<<span>int</span>> data, <span>int</span> maxIndex, <span>int</span> left, <span>int</span> right, <span>int</span><span> lastIndex) </span><span>43</span> <span> { </span><span>44</span> <span>int</span><span> temp; </span><span>45</span> <span>if</span> (right ><span> lastIndex) <span>//父节点只有一个子节点 </span></span><span>46</span> temp =<span> left; </span><span>47</span> <span>else</span> <span>48</span> <span> { </span><span>49</span> <span>if</span> (data[left] ><span> data[right]) </span><span>50</span> temp =<span> left; </span><span>51</span> <span>else</span> temp =<span> right; </span><span>52</span> <span> } </span><span>53</span> <span>54</span> <span>if</span> (data[maxIndex] <<span> data[temp]) </span><span>55</span> <span> Swap(data, maxIndex, temp); </span><span>56</span> <span>else</span> temp =<span> maxIndex; </span><span>57</span> <span>return</span><span> temp; </span><span>58</span> }
|
过程解析:BuildMaxHeapify为排序前构建的最大堆数列方法,主要内容为从最后一个父节点开始往前将每个三角组合
(即父节点与它的两个子节点)符合父节点值最大的规则。ReSortMaxBranch为将三角调整为父节点值最大,
并返回该值之前的索引,用来判断是否进行了交换,以及原来的父节点值交换到了什么位置。在HeapSort里首先
构建了最大堆数列,然后将根节点交换到末尾,根节点不是最大值了,在while语句中对最大堆数列进行调整。
插曲:自从看了Martin Fowler大师《重构》第三版,我发现我更不喜欢写注释了。每次都想着尽量让方法的名字更贴切,
即使会造成方法的名字很长很丑。这算不算曲解了大师的意思啊!?上面的代码注释都是写博客的时候现加的(源代码很干净的。汗!)。
希尔排序
希尔排序是插入排序的一种更高效的改进版本。
在前面介绍的插入排序,我们知道1.它对有序数列排序的效率是非常高的 2.要排序的数向前移动是一步步进行的导致插入排序效率低。
希尔排序正是利用第一点,改善第二点,达到更理想的效果。
原理:通过奇妙的步长,插入排序间隔步长的元素,随后逐渐缩短步长至1,实现数列的插入排序。 维基入口
疑问:可以想象到排序间隔步长的数,会逐渐让数列变得有序,提升最后步长为1时标准插入排序的效率。在维基上看到这么
一句话“可能希尔排序最重要的地方在于当用较小步长排序后,以前用的较大步长仍然是有序的”注意用词是‘可能’。我的疑问是
这是个正确的命题吗?如何证明呢?看维基上也是由果推因,说是如果不是这样,就不会排序那么快了。可这我感觉还是太牵强了,
哪位大哥发现相关资料,希望能分享出来,不胜感激。
实现如下:
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
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> ShellSort(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span>int</span><span> temp; </span><span> 4</span> <span>for</span> (<span>int</span> gap = data.Count / <span>2</span>; gap > <span>0</span>; gap /= <span>2</span><span>) </span><span> 5</span> <span> { </span><span> 6</span> <span>for</span> (<span>int</span> i = gap; i < data.Count; i +=<span> gap) </span><span> 7</span> <span> { </span><span> 8</span> temp =<span> data[i]; </span><span> 9</span> <span>for</span> (<span>int</span> j = i - gap; j >= <span>0</span>; j -=<span> gap) </span><span>10</span> <span> { </span><span>11</span> <span>if</span> (data[j] ><span> temp) </span><span>12</span> <span> { </span><span>13</span> data[j + gap] =<span> data[j]; </span><span>14</span> <span>if</span> (j == <span>0</span><span>) </span><span>15</span> <span> { </span><span>16</span> data[j] =<span> temp; </span><span>17</span> <span>break</span><span>; </span><span>18</span> <span> } </span><span>19</span> <span> } </span><span>20</span> <span>else</span> <span>21</span> <span> { </span><span>22</span> data[j + gap] =<span> temp; </span><span>23</span> <span>break</span><span>; </span><span>24</span> <span> } </span><span>25</span> <span> } </span><span>26</span> <span> } </span><span>27</span> <span> } </span><span>28</span> }
|
过程解析:采用的步长是N/2,每次取半,直至1。循环内部就是标准的插入排序。
——————
修正:修正后希尔排序才是真正牛叉的希尔啊!感谢@390218462的提出
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
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> ShellSortCorrect(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span>int</span><span> temp; </span><span> 4</span> <span>for</span> (<span>int</span> gap = data.Count / <span>2</span>; gap > <span>0</span>; gap /= <span>2</span><span>) </span><span> 5</span> <span> { </span><span> 6</span> <span>for</span> (<span>int</span> i = gap; i < data.Count; <span>i++</span><span>) // <span>i+ = gap 改为了 i++ </span></span><span> 7</span> <span> { </span><span> 8</span> temp =<span> data[i]; </span><span> 9</span> <span>for</span> (<span>int</span> j = i - gap; j >= <span>0</span>; j -=<span> gap) </span><span>10</span> <span> { </span><span>11</span> <span>if</span> (data[j] ><span> temp) </span><span>12</span> <span> { </span><span>13</span> data[j + gap] =<span> data[j]; </span><span>14</span> <span>if</span> (j == <span>0</span><span>) </span><span>15</span> <span> { </span><span>16</span> data[j] =<span> temp; </span><span>17</span> <span>break</span><span>; </span><span>18</span> <span> } </span><span>19</span> <span> } </span><span>20</span> <span>else</span> <span>21</span> <span> { </span><span>22</span> data[j + gap] =<span> temp; </span><span>23</span> <span>break</span><span>; </span><span>24</span> <span> } </span><span>25</span> <span> } </span><span>26</span> <span> } </span><span>27</span> <span> } </span><span>28</span> }
|
——————
这里实现的貌似是最差的希尔排序。主要源于步长的选择。维基上有各种牛叉的“凌波微步”,极限在哪里,
喜欢挑战的同学可以去学习学习。看维基排序算法里六种排序的测试,希尔最快,比快速排序还快!!我没实现啊!
只是对于神奇的步长更充满了敬畏。
基数排序
基数排序是一种非比较型整数排序。
“非比较型”是什么意思呢?因为它内部使用的是桶排序,而桶排序是非比较型排序。
这里就要说说桶排序了。一个非常有意思的排序。
桶排序
原理:取一定数量(数列中的最大值)的编好序号的桶,将数列每个数放进编号为它的桶里,然后将不是空的桶依次倒出来,
就组成有序数列了。 维基入口
好吧!聪明的人一眼就看出桶排序的破绽了。假设只有两个数1,10000,岂不是要一万个桶!?这确实是个问题啊!我也
没想出解决办法。我起初也以为桶排序就是一个通过牺牲空间来换取时间的排序算法,它不需要比较,所以是非比较型算法。
但看了有趣的演示的桶排序后,发现世界之大,你没有解决,不代表别人没解决,睿智的人总是很多。
1,9999的桶排序实现:new Int[2];总共有两个数,得出最大数9999的位数4,取10的4次幂即10000作为分母,
要排序的数(1或9999)作为分子,并乘以数列总数2,即1*2/10000,9999*2/10000得到各自的位置0,1,完成排序。
如果是1,10000进行排序的话,上面的做法就需要稍微加一些处理——发现最大数是10的n次幂,就将它作为分母,并
放在数列末尾就好了。
如果是9999,10000进行排序的话,那就需要二维数组了,两个都在位置1,位置0没数。这个时候就需要在放
入每个位置时采用其它排序(比如插入排序)办法对这个位置的多个数排序了。
为基数排序做个过渡,我这里实现了一个个位数桶排序
涉及到了当重复的数出现的处理。
实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <span> 1</span> <span>public</span> <span>static</span> <span>void</span> BucketSortOnlyUnitDigit(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span>int</span>[] indexCounter = <span>new</span> <span>int</span>[<span>10</span><span>]; </span><span> 4</span> <span>for</span> (<span>int</span> i = <span>0</span>; i < data.Count; i++<span>) </span><span> 5</span> <span> { </span><span> 6</span> indexCounter[data[i]]++<span>; </span><span> 7</span> <span> } </span><span> 8</span> <span>int</span>[] indexBegin = <span>new</span> <span>int</span>[<span>10</span><span>]; </span><span> 9</span> <span>for</span> (<span>int</span> i = <span>1</span>; i < <span>10</span>; i++<span>) </span><span>10</span> <span> { </span><span>11</span> indexBegin[i] = indexBegin[i-1]+<span> indexCounter[i-1]; </span><span>15</span> <span> } </span><span>16</span> IList<<span>int</span>> tempList =<span> NewInstance(data, data.Count); </span><span>17</span> <span>for</span> (<span>int</span> i = <span>0</span>; i < data.Count; i++<span>) </span><span>18</span> <span> { </span><span>19</span> <span>int</span> number =<span> data[i]; </span><span>20</span> tempList[indexBegin[number]++] =<span> data[i]; </span><span>21</span> <span> } </span><span>22</span> data =<span> tempList; </span><span>23</span> }
|
过程解析:indexCounter进行对每个数出现的频率的统计。indexBegin存储每个数的起始索引。
比如 1 1 2,indexCounter统计到0个0,2个1,1个2。indexBegin计算出0,1,2的起始索引分别为
0,0,2。当1个1已取出排序,那索引将+1,变为0,1,2。这样就通过提前给重复的数空出位置,解决了
重复的数出现的问题。当然,你也可以考虑用二维数组来解决重复。
下面继续基数排序。
基数排序原理:将整数按位数切割成不同的数字,然后按每个位数分别比较。
取得最大数的位数,从低位开始,每个位上进行桶排序。
实现如下:
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
| <span> 1</span> <span>public</span> <span>static</span> IList<<span>int</span>> RadixSort(IList<<span>int</span>><span> data) </span><span> 2</span> <span> { </span><span> 3</span> <span>int</span> max = data[<span>0</span><span>]; </span><span> 4</span> <span>for</span> (<span>int</span> i = <span>1</span>; i < data.Count; i++<span>) </span><span> 5</span> <span> { </span><span> 6</span> <span>if</span> (data[i] ><span> max) </span><span> 7</span> max =<span> data[i]; </span><span> 8</span> <span> } </span><span> 9</span> <span>int</span> digit = <span>1</span><span>; </span><span>10</span> <span>while</span> (max / <span>10</span> != <span>0</span><span>) </span><span>11</span> <span> { </span><span>12</span> digit++<span>; </span><span>13</span> max /= <span>10</span><span>; </span><span>14</span> <span> } </span><span>15</span> <span>for</span> (<span>int</span> i = <span>0</span>; i < digit; i++<span>) </span><span>16</span> <span> { </span><span>17</span> <span>int</span>[] indexCounter = <span>new</span> <span>int</span>[<span>10</span><span>]; </span><span>18</span> IList<<span>int</span>> tempList =<span> NewInstance(data, data.Count); </span><span>19</span> <span>for</span> (<span>int</span> j = <span>0</span>; j < data.Count; j++<span>) </span><span>20</span> <span> { </span><span>21</span> <span>int</span> number = (data[j] % Convert.ToInt32(Math.Pow(<span>10</span>, i + <span>1</span>))) / Convert.ToInt32(Math.Pow(<span>10</span>, i)); <span>//</span><span>得出i+1位上的数</span> <span>22</span> indexCounter[number]++<span>; </span><span>23</span> <span> } </span><span>24</span> <span>int</span>[] indexBegin = <span>new</span> <span>int</span>[<span>10</span><span>]; </span><span>25</span> <span>for</span> (<span>int</span> k = <span>1</span>; k < <span>10</span>; k++<span>) </span><span>26</span> <span> { </span><span>27</span> indexBegin[k] = indexBegin[k - <span>1</span>] + indexCounter[k - <span>1</span><span>]; </span><span>28</span> <span> } </span><span>29</span> <span>for</span> (<span>int</span> k = <span>0</span>; k < data.Count; k++<span>) </span><span>30</span> <span> { </span><span>31</span> <span>int</span> number = (data[k] % Convert.ToInt32(Math.Pow(<span>10</span>, i + <span>1</span>))) / Convert.ToInt32(Math.Pow(<span>10</span><span>, i)); </span><span>32</span> tempList[indexBegin[number]++] =<span> data[k]; </span><span>33</span> <span> } </span><span>34</span> data =<span> tempList; </span><span>35</span> <span> } </span><span>36</span> <span>return</span><span> data; </span><span>37</span> }
|
过程解析:得出最大数的位数,从低位开始桶排序。我写的这个实现代码并不简洁,但逻辑更清晰。
后面测试的时候我们就会发现,按理来说这个实现也还行吧! 但并不如想象的那么快!
循环的次数太多?(统计频率n次+9次计算+n次放到新的数组)*位数。
创建的新实例太多?(new int[10]两次+NewInstance is反射判断创建实例+new int[n])*位数
测试比较
添加随机数组,数组有序校验,微软Linq排序
代码如下:
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
| <span> 1</span> <span>public</span> <span>static</span> <span>int</span>[] RandomSet(<span>int</span> length, <span>int</span><span> max) </span><span> 2</span> <span> { </span><span> 3</span> <span>int</span>[] result = <span>new</span> <span>int</span><span>[length]; </span><span> 4</span> Random rand = <span>new</span><span> Random(); </span><span> 5</span> <span>for</span> (<span>int</span> i = <span>0</span>; i < result.Length; i++<span>) </span><span> 6</span> <span> { </span><span> 7</span> result[i] =<span> rand.Next(max); </span><span> 8</span> <span> } </span><span> 9</span> <span>return</span><span> result; </span><span>10</span> <span> } </span><span>11</span> <span>12</span> <span>public</span> <span>static</span> <span>bool</span> IsAscOrdered(IList<<span>int</span>><span> data) </span><span>13</span> <span> { </span><span>14</span> <span>bool</span> flag = <span>true</span><span>; </span><span>15</span> <span>for</span> (<span>int</span> i = <span>0</span>; i < data.Count - <span>1</span>; i++<span>) </span><span>16</span> <span> { </span><span>17</span> <span>if</span> (data[i] > data[i + <span>1</span><span>]) </span><span>18</span> flag = <span>false</span><span>; </span><span>19</span> <span> } </span><span>20</span> <span>return</span><span> flag; </span><span>21</span> <span> } </span><span>22</span> <span>23</span> <span>public</span> <span>static</span> <span>void</span> TestMicrosoft(IList<<span>int</span>><span> data) </span><span>24</span> <span> { </span><span>25</span> Stopwatch stopwatch = <span>new</span><span> Stopwatch(); </span><span>26</span> <span> stopwatch.Start(); </span><span>27</span> List<<span>int</span>> result = data.OrderBy(a =><span> a).ToList(); </span><span>28</span> <span> stopwatch.Stop(); </span><span>29</span> <span>string</span> methodName = <span>"</span><span>TestMicrosoft</span><span>"</span><span>; </span><span>30</span> <span>int</span> length =<span> methodName.Length; </span><span>31</span> <span>for</span> (<span>int</span> i = <span>0</span>; i < <span>40</span> - length; i++<span>) </span><span>32</span> <span> { </span><span>33</span> methodName += <span>"</span> <span>"</span><span>; </span><span>34</span> <span> } </span><span>35</span> Console.WriteLine(methodName + <span>36</span> <span>"</span><span> IsAscOrdered:</span><span>"</span> + IsAscOrdered(result) + <span>"</span><span> Time:</span><span>"</span> +<span> stopwatch.Elapsed.TotalSeconds); </span><span>37</span> <span>38</span> }
|
测试主体如下:
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
| <span> 1</span> <span>static</span> <span>void</span> Main(<span>string</span><span>[] args) </span><span> 2</span> <span> { </span><span> 3</span> <span>int</span>[] aa = RandomSet(<span>50000</span>, <span>99999</span><span>); </span><span> 4</span> <span>//</span><span>int[] aa = OrderedSet(5000);</span> <span> 5</span> Console.WriteLine(<span>"</span><span>Array Length:</span><span>"</span> +<span> aa.Length); </span><span> 6</span> RunTheMethod((Action<IList<<span>int</span>>>)SelectSort, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span> 7</span> RunTheMethod((Action<IList<<span>int</span>>>)BubbleSort, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span> 8</span> RunTheMethod((Action<IList<<span>int</span>>>)BubbleSortImprovedWithFlag, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span> 9</span> RunTheMethod((Action<IList<<span>int</span>>>)BubbleCocktailSort, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>10</span> RunTheMethod((Action<IList<<span>int</span>>>)InsertSort, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>11</span> RunTheMethod((Action<IList<<span>int</span>>>)InsertSortImprovedWithBinarySearch, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>12</span> RunTheMethod((Action<IList<<span>int</span>>>)QuickSortStrict, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>13</span> RunTheMethod((Action<IList<<span>int</span>>>)QuickSortRelax, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>14</span> RunTheMethod((Action<IList<<span>int</span>>>)QuickSortRelaxImproved, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>15</span> RunTheMethod((Func<IList<<span>int</span>>, IList<<span>int</span>>>)MergeSort, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>16</span> RunTheMethod((Action<IList<<span>int</span>>>)ShellSort, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>17</span> RunTheMethod((Func<IList<<span>int</span>>, IList<<span>int</span>>>)RadixSort, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>18</span> RunTheMethod((Action<IList<<span>int</span>>>)HeapSort, aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>19</span> TestMicrosoft(aa.Clone() <span>as</span> <span>int</span><span>[]); </span><span>20</span> <span> Console.Read(); </span><span>21</span> <span> } </span><span>22</span> <span>23</span> <span>public</span> <span>static</span> <span>void</span> RunTheMethod(Func<IList<<span>int</span>>, IList<<span>int</span>>> method, IList<<span>int</span>><span> data) </span><span>24</span> <span> { </span><span>25</span> Stopwatch stopwatch = <span>new</span><span> Stopwatch(); </span><span>26</span> <span> stopwatch.Start(); </span><span>27</span> IList<<span>int</span>> result =<span> method(data); </span><span>28</span> <span> stopwatch.Stop(); </span><span>29</span> <span>string</span> methodName =<span> method.Method.Name; </span><span>30</span> <span>int</span> length =<span> methodName.Length; </span><span>31</span> <span>for</span> (<span>int</span> i = <span>0</span>; i < <span>40</span> - length; i++<span>) </span><span>32</span> <span> { </span><span>33</span> methodName += <span>"</span> <span>"</span><span>; </span><span>34</span> <span> } </span><span>35</span> Console.WriteLine(methodName + <span>36</span> <span>"</span><span> IsAscOrdered:</span><span>"</span> + IsAscOrdered(result) + <span>"</span><span> Time:</span><span>"</span> +<span> stopwatch.Elapsed.TotalSeconds); </span><span>37</span> <span> } </span><span>38</span> <span>39</span> <span>public</span> <span>static</span> <span>void</span> RunTheMethod(Action<IList<<span>int</span>>> method, IList<<span>int</span>><span> data) </span><span>40</span> <span> { </span><span>41</span> Stopwatch stopwatch = <span>new</span><span> Stopwatch(); </span><span>42</span> <span> stopwatch.Start(); </span><span>43</span> <span> method(data); </span><span>44</span> <span> stopwatch.Stop(); </span><span>45</span> <span>string</span> methodName =<span> method.Method.Name; </span><span>46</span> <span>int</span> length =<span> methodName.Length; </span><span>47</span> <span>for</span> (<span>int</span> i = <span>0</span>; i < <span>40</span> - length; i++<span>) </span><span>48</span> <span> { </span><span>49</span> methodName += <span>"</span> <span>"</span><span>; </span><span>50</span> <span> } </span><span>51</span> Console.WriteLine(methodName + <span>52</span> <span>"</span><span> IsAscOrdered:</span><span>"</span> + IsAscOrdered(data) + <span>"</span><span> Time:</span><span>"</span> +<span> stopwatch.Elapsed.TotalSeconds); </span><span>53</span> }
|
剩余代码折叠在此处
View Code
测试设备:win8(64位),i7-3630QM,8G内存,vs2012
测试结果:
100000,50000,10000,5000,1000,100依次是:
结果分析:可以看出在大数组的时候,微软自带排序更接近快速排序。而当数组变小时,速度却没有明显提升,甚至变得更慢,
比如1000和100。可以推断出在数组足够小的时候,比较已经不是影响这个方法主要因素。而根据它对大数组的表现。我们可以
推断出它应该用的是快速排序。反编译验证下:
在System.Linq.EnumerableSorter下。有兴趣的同学可以去看下详细实现。
维基上也有个测试。硬件没我的好。时间是我测试结果时间的几百倍。有兴趣的同学可以比较下。
在上面的测试中,我们可以看到快速最快,归并其次,冒泡最慢(维基上是希尔最快,估计使用的是某种神奇的步长)。
在我这里,以前实现的希尔还不如二分查找优化版的快,修正后希尔快了相当多,上面测试的希尔排序是以前错误的实现。
修正后的实现测试效果请点击右侧导航到希尔排序查看。希尔排序是一种神奇又有潜力的算法。步长不好会很挫!
而基数排序却是比平均时间复杂度为o(nlogn)的堆排序,归并排序,快速排序还要慢的,虽然它的平均时间复杂度为o(n)。
冒泡标识优化版对随机数列结果优化不明显,鸡尾酒版优化可以看到,但也不是很厉害。
插入排序二分查找优化版优化比较明显。我优化的快速排序QuickSortRelaxImproved优化也不明显。
以上是随机数列的测试结果,最大值为99999。
而对于有序数列,这些方法表现又会如何呢?
我这里就不演示了。本文末尾会附上demo,大家可以自行测试。
有意思的是:
我在测试有序数列的时候,QuickSortStrict方法栈溢出了(stack overflow exception)。这个异常
是让我去stackoverflow搜寻答案吗?哈哈!我确信我的方法不是无限循环。跳过一堆链接。。。我是
在测试10000个数排序的时候发生的错误。我跟踪后发现大约在9400多次递归的时候,栈溢出。找啊找
终于找见了一个类似的问题。上面说如果一个递归9000多次而没有返回值,也会报栈溢出的。而这个方法
对于10000个有序数列,确实每次减少一个数地递归,次数会超过限制。
我的算法理论不怎么好,对于时间复杂度和空间复杂度,还有稳定度,搞得也不怎么清楚,只知道个大致的
意思。各位要笔试面试的朋友可以去维基百科这个表来了解学习。
总结
我觉得使用IList更贴近数列,更能展现基本的操作。所以我的实现中都没有将它强制转化为List
或者int[]来调用微软封装的方法。这样说来,题目说C#实现倒快有点名不副实了。不过这样却也方便了其它语言
朋友。比如将我这篇博文里的实现随便改改,就可以说是另一个语言版本的8种排序算法了。哈哈!在这里,
我想说下这次学习排序对我的意义:老久不怎么动脑了,突然动起来,磨磨唧唧地得出结果,最后倒也有点成就感!
在学习过程中,经常会脑子转不过弯,想不通的,只是走在路上或者睡觉前突然灵感一现,有点小惊喜的感觉!
这大概就是进步的特征吧!哈哈!这次写demo+写博客花费了不少时间,倒也收获颇多,尤其在我将8种
排序都实现之前,没进行过一次测试,全部实现完成后,测试时各种索引越界+无限循环+各种问题,没几个
能跑通的,到后来的几乎都没有问题,也算是锻炼了思维,找出错原因的能力。本篇是自我学习的一个总结,
要学习及锻炼的园友,还望一定自己实现一下,可以和我的比较一下,解除疑惑或者提出改进。
PS:我打算三月份去广州发展,主要会Asp.net mvc+jquery(不介意学习新的技术[除了webform]及语言[除了java])。