posts - 30, comments - 120, trackbacks - 1, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

置顶随笔

     摘要: 最近研究算法,把常用的算法用C#实现了。整理了一下列在这里,以备查找。排序算法交换排序冒泡排序快速排序奇偶排序鸡尾酒排序Gnome SortComb Sort插入排序插入排序 希尔排序选择排序选择排序 堆排序合并排序合并排序 Strand排序搜索算法二分搜索Interpolation搜索jump搜索非线性搜索快速选择算法ternary选择算法Uniform二分搜索混淆(洗牌算法)KnuthShuf...  阅读全文

posted @ 2009-12-19 17:24 smallnest 阅读(66) | 评论 (0)编辑

2009年12月19日


希尔排序是一种插入排序法,它出自D.L.Shell,因此而得名。Shell排序又称作缩小增量排序。
  基本思想:
  不断把待排序的对象分成若干个小组,对同一小组内的对象采用直接插入法排序,当完成了所有对象都分在一个组内的排序后,排序过程结束。每次比较指定间距的两个数据项,若左边的值小于右边的值,则交换它们的位置。间距d按给定公式减少: di+1=(di +1)/2 ,直到d等于1为止。D可以选取{9,5,3,2,1}。

代码
1 using System;
2  using System.Collections.Generic;
3
4  namespace Com.Colobu.Algorithm.Insertion
5 {
6 /// <summary>
7 /// <b>Shell sort</b> is a sorting algorithm that is a generalization of insertion sort,
8 /// with two observations:
9 /// insertion sort is efficient if the input is "almost sorted"
10 /// insertion sort is typically inefficient because it moves values just one position at a time.
11 ///
12 /// 对有n个元素的可比较资料,先取一个小于n的整数d1作为第一个增量,
13 /// 把文件的全部记录分成d1个组。
14 /// 所有距离为d1的倍数的记录放在同一个组中。
15 /// 先在各组内进行直接插入排序;然后,取第二个增量d2<d1
16 /// 重复上述的分组和排序,直至所取的增量dt=1(dt<dt-1<…<d2<d1),
17 /// 即所有记录放在同一组中进行直接插入排序为止。
18 /// 该方法实质上是一种分组插入方法。
19 ///
20 /// 平均时间复杂度:O(nlogn)
21 /// Stability:No
22 /// </summary>
23 public class ShellSortAlgorithm
24 {
25 public static void ShellSort<T>(IList<T> szArray) where T : IComparable
26 {
27 int i, j, k, gap;
28 T temp;
29
30 //gap sequence:a(n) = floor(fibonacci(n+1)^(1+sqrt(5)))
31 //the Fibonacci numbers (leaving out one of the starting 1's) to the power of twice the golden ratio
32 int[] gaps = { 1,5,13,43,113,297,815,1989,4711,11969,27901,84801,
33 213331,543749,1355339,3501671,8810089,21521774,
34 58548857,157840433,410151271,1131376761,2147483647 };
35
36 int n = szArray.Count;
37
38 //从素数序列中得到一个满足条件的最大的素数
39 for (k = 0; gaps[k] < n; k++) ;
40
41 while (--k >= 0)
42 {
43 gap = gaps[k]; //取增量
44
45 for (i = gap; i < n; i++) //按照此增量分组
46 {
47 temp = szArray[i];
48 j = i;
49 while (j >= gap && szArray[j - gap].CompareTo(temp) > 0) //将此组插入排序
50 {
51 szArray[j] = szArray[j - gap];
52 j = j - gap;
53 }
54 szArray[j] = temp;
55 }
56 }
57
58 }
59 }
60 }
61

 

posted @ 2009-12-19 17:41 smallnest 阅读(12) | 评论 (0)编辑

插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法。
它的工作原理是通过构建有序序列,对于未排序数据,
 在已排序序列中从后向前扫描,找到相应位置并插入。
插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),
因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,
为最新元素提供插入空间。

代码
1 using System;
2  using System.Collections.Generic;
3
4 namespace Com.Colobu.Algorithm.Insertion
5 {
6 /// <summary>
7 /// 插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法。
8 /// 它的工作原理是通过构建有序序列,对于未排序数据,
9 /// 在已排序序列中从后向前扫描,找到相应位置并插入。
10 /// 插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),
11 /// 因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,
12 /// 为最新元素提供插入空间。
13 ///
14 /// 平均时间复杂度:O(n^2)
15 /// Stability:Yes
16 /// </summary>
17 public class InsertionSortAlgorithm
18 {
19 public static void InsertionSort<T>(IList<T> szArray) where T : IComparable
20 {
21 int count = szArray.Count;
22 int j;
23 T temp;
24 for (int i = 1; i < count; i++)
25 {
26 temp = szArray[i];//store the original sorted array in temp
27 for (j = i; j > 0 && (temp.CompareTo(szArray[j - 1]) < 0); j--)//compare the new array with temp
28 {
29 szArray[j] = szArray[j - 1];//all larger elements are moved one pot to the right
30 }
31 szArray[j] = temp;
32 }
33
34 }
35 }
36 }
37

 

posted @ 2009-12-19 17:39 smallnest 阅读(8) | 评论 (0)编辑

类别:排序-交换排序
参看 维基百科的定义

1 using System;
2  using System.Collections.Generic;
3
4 namespace Com.Colobu.Algorithm.Exchange
5 {
6 /// <summary>
7 /// <b>Comb sort</b> improves on bubble sort, and rivals algorithms like Quicksort.
8 /// The basic idea is to eliminate turtles, or small values near the end of the list,
9 /// since in a bubble sort these slow the sorting down tremendously.
10 ///
11 /// 平均时间复杂度:O(nlogn)
12 /// Stability:No
13 /// </summary>
14 public class CombSortAlgorithm
15 {
16 public static void CombSort<T>(IList<T> szArray) where T : IComparable
17 {
18 int gap = szArray.Count;
19 bool swapped = true;
20
21 while (gap > 1 || swapped)
22 {
23 if (gap > 1)
24 {
25 gap = (int)(gap / 1.25);
26 }
27
28 int i = 0;
29 swapped = false;
30 while (i + gap < szArray.Count)
31 {
32 if (szArray[i].CompareTo(szArray[i + gap]) > 0)
33 {
34 T t = szArray[i];
35 szArray[i] = szArray[i + gap];
36 szArray[i + gap] = t;
37 swapped = true;
38 }
39 i++;
40 }
41 }
42 }
43 }
44 }
45

 

posted @ 2009-12-19 17:33 smallnest 阅读(14) | 评论 (0)编辑

类别:排序-交换排序
参看 维基百科的定义

Gnome sort is a sorting algorithm which is similarto insertion sort, except that moving an element to its proper place isaccomplished by a series of swaps, as in bubble sort. The name comesfrom the supposed behavior of the Dutch garden gnome in sorting a lineof flowerpots and is described on Dick Grune's Gnome sort page.

1 using System;
2  using System.Collections.Generic;
3
4 namespace Com.Colobu.Algorithm.Exchange
5 {
6 /// <summary>
7 /// <b>Gnome sort</b> is a sorting algorithm which is similar to insertion sort,
8 /// except that moving an element to its proper place is accomplished
9 /// by a series of swaps, as in bubble sort.
10 /// Gnome Sort is based on the technique used by the standard Dutch Garden Gnome.
11 /// Here is how a garden gnome sorts a line of flower pots.
12 /// Basically, he looks at the flower pot next to him and the previous one;
13 /// if they are in the right order he steps one pot forward,
14 /// otherwise he swaps them and steps one pot backwards.
15 /// Boundary conditions: if there is no previous pot,
16 /// he steps forwards; if there is no pot next to him, he is done.
17 ///
18 /// 平均时间复杂度:O(n^2)
19 /// Stability:Yes
20 /// </summary>
21 public class GnomeSortAlgorithm
22 {
23 public static void GnomeSort<T>(IList<T> szArray) where T : IComparable
24 {
25 int i = 1;
26 int j = 2;
27 int count = szArray.Count;
28 while (i < count)
29 {
30 if (szArray[i - 1].CompareTo(szArray[i]) < 0) //for descending sort, reverse the comparison to >=
31 {
32 i = j;
33 j = j + 1;
34 }
35 else
36 {
37 Swap(szArray, i - 1, i);
38 i = i - 1;
39 if (i == 0)
40 {
41 i = j;
42 j = j + 1;
43 }
44 }
45 }
46 }
47 private static void Swap<T>(IList<T> szArray, int i, int j)
48 {
49 T tmp = szArray[i];
50 szArray[i] = szArray[j];
51 szArray[j] = tmp;
52 }
53 }
54 }
55

 


posted @ 2009-12-19 17:32 smallnest 阅读(7) | 评论 (0)编辑

类别:排序-交换排序
参看 维基百科的定义

鸡尾酒排序,也就是定向冒泡排序, 鸡尾酒搅拌排序, 搅拌排序 (也可以视作选择排序的一种变形), 涟漪排序, 来回排序 or 快乐小时排序, 是冒泡排序的一种变形。此算法与冒泡排序的不同处在于排序时是以双向在序列中进行排序。

1 using System;
2  using System.Collections.Generic;
3
4 namespace Com.Colobu.Algorithm.Exchange
5 {
6 /// <summary>
7 /// <b>鸡尾酒排序</b>,也就是双向冒泡排序(bidirectional bubble sort), 鸡尾酒搅拌排序, 搅拌排序
8 /// (也可以视作选择排序的一种变形), 涟漪排序, 来回排序 or 快乐小时排序,
9 /// 是冒泡排序的一种变形。
10 /// 此算法与冒泡排序的不同处在于排序时是以双向在序列中进行排序。
11 ///
12 /// 平均时间复杂度:O(n^2)
13 /// Stability:Yes
14 /// </summary>
15 public class CocktailSortAlgorithm
16 {
17 public static void CocktailSort<T>(IList<T> szArray) where T : IComparable
18 {
19 int bottom = 0;
20 int top = szArray.Count - 1;
21 bool swapped = true;
22 while (swapped == true) // if no elements have been swapped, then the list is sorted
23 {
24 swapped = false;
25 for (int i = bottom; i < top; i = i + 1)
26 {
27 if (szArray[i].CompareTo(szArray[i + 1]) > 0) // test whether the two elements are in the correct order
28 {
29 Swap(szArray,i,i + 1); // let the two elements change places
30 swapped = true;
31 }
32 }
33 // decreases top the because the element with the largest value in the unsorted
34 // part of the list is now on the position top
35 top = top - 1;
36 for (int i = top; i > bottom; i = i - 1)
37 {
38 if (szArray[i].CompareTo(szArray[i - 1]) < 0)
39 {
40 Swap(szArray,i,i - 1);
41 swapped = true;
42 }
43 }
44 // increases bottom because the element with the smallest value in the unsorted
45 // part of the list is now on the position bottom
46 bottom = bottom + 1;
47 }
48 }
49
50 private static void Swap<T>(IList<T> szArray, int i,int j)
51 {
52 T tmp = szArray[i];
53 szArray[i] = szArray[j];
54 szArray[j] = tmp;
55 }
56 }
57 }
58

 

posted @ 2009-12-19 17:31 smallnest 阅读(4) | 评论 (0)编辑

 

1 using System;
2  using System.Collections.Generic;
3
4
5  namespace Com.Colobu.Algorithm.Exchange
6 {
7 /// <summary>
8 /// <b>奇偶排序</b>的思路是在数组中重复两趟扫描。
9 /// 第一趟扫描选择所有的数据项对,a[j]和a[j+1],j是奇数(j=1, 3, 5……)。
10 /// 如果它们的关键字的值次序颠倒,就交换它们。
11 /// 第二趟扫描对所有的偶数数据项进行同样的操作(j=2, 4,6……)。
12 /// 重复进行这样两趟的排序直到数组全部有序。
13 ///
14 /// 平均时间复杂度:O(n^2)
15 /// Stability:Yes
16 /// </summary>
17 public class OddEvenSortAlgorithm
18 {
19 public static void OddEvenSort<T>(IList<T> szArray) where T : IComparable
20 {
21 bool sorted = false;
22 while (!sorted)
23 {
24 sorted = true;
25 // odd-even
26 for (int i = 1; i < szArray.Count - 1; i += 2)
27 {
28 if (szArray[i].CompareTo(szArray[i + 1]) > 0)
29 {
30 Swap(szArray, i, i + 1);
31 sorted = false;
32 }
33 }
34 // even-odd
35 for (int j = 0; j < szArray.Count - 1; j += 2)
36 {
37 if (szArray[j].CompareTo(szArray[j + 1]) > 0)
38 {
39 Swap(szArray, j, j + 1);
40 sorted = false;
41 }
42 }
43 }
44 }
45 private static void Swap<T>(IList<T> szArray, int i, int j)
46 {
47 T tmp = szArray[i];
48 szArray[i] = szArray[j];
49 szArray[j] = tmp;
50 }
51 }
52 }
53

 

类别:排序-交换排序
参看 维基百科的定义

posted @ 2009-12-19 17:29 smallnest 阅读(17) | 评论 (0)编辑

类别:排序-交换排序
参看 维基百科的定义

1 using System;
2  using System.Collections.Generic;
3
4 namespace Com.Colobu.Algorithm.Exchange
5 {
6 /// <summary>
7 /// <b>快速排序</b>是所有排序算法中最高效的一种.
8 /// 它采用了分治的思想:先保证列表的前半部分都小于后半部分,
9 /// 然后分别对前半部分和后半部分排序,这样整个列表就有序了。
10 /// 这是一种先进的思想,也是它高效的原因。
11 /// 因为在排序算法中,算法的高效与否与列表中数字间的比较次数有直接的关系,
12 /// 而"保证列表的前半部分都小于后半部分"就使得前半部分的任何一个数从此以后
13 /// 都不再跟后半部分的数进行比较了,大大减少了数字间不必要的比较。
14 ///
15 /// 平均时间复杂度:O(nlogn)
16 /// Stability:No
17 /// </summary>
18 public class QuickSortAlgorithm
19 {
20 // QuickSort implementation
21 public static void QuickSort<T>(IList<T> szArray, int nLower, int nUpper) where T : IComparable
22 {
23 if (nLower < nUpper)
24 {
25 int nSplit = Partition(szArray, nLower, nUpper);
26 QuickSort(szArray, nLower, nSplit - 1);
27 QuickSort(szArray, nSplit + 1, nUpper);
28 }
29 }
30 // QuickSort partition implementation
31 private static int Partition<T>(IList<T> szArray, int nLower, int nUpper) where T:IComparable
32 {
33 T szPivot = szArray[nLower]; //分隔点的值,用数字第一个值代替
34
35 int nLeft = nLower + 1;//左边开始点
36 int nRight = nUpper; //右边开始点
37
38 T szSwap; //交换变量
39
40 //开始查找
41 //将小于分隔点的值都挪到左边
42 //将小于分隔点的值都挪到右边
43 while (nLeft <= nRight)
44 {
45 //找到左边第一个大于分隔点的值
46 while (nLeft <= nRight && szArray[nLeft].CompareTo(szPivot) <= 0)
47 nLeft = nLeft + 1;
48
49 //找到右边第一个小于分隔点的值
50 while (nLeft <= nRight && szArray[nRight].CompareTo(szPivot) > 0)
51 nRight = nRight - 1;
52
53 //如果左右还没有交叉(碰头),交换左右两个值
54 if (nLeft < nRight)
55 {
56 szSwap = szArray[nLeft];
57 szArray[nLeft] = szArray[nRight];
58 szArray[nRight] = szSwap;
59 nLeft = nLeft + 1;
60 nRight = nRight - 1;
61 }
62 }
63
64 //将分隔点移动到最后那个点,此时nRight = nLeft - 1
65 //所以那个点的值小于分隔点,可以与分隔点进行交换
66 szSwap = szArray[nLower];
67 szArray[nLower] = szArray[nRight];
68 szArray[nRight] = szSwap;
69
70 //现在在nRight左边的值都小于分隔点,nRight右边的值都大于分隔点
71 return nRight;
72 }
73
74 }
75 }
76

 

posted @ 2009-12-19 17:27 smallnest 阅读(9) | 评论 (0)编辑

冒泡排序(Bubble Sort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。

代码
1 using System;
2  using System.Collections.Generic;
3
4 namespace Com.Colobu.Algorithm.Exchange
5 {
6 /// <summary>
7 /// 冒泡排序是这样实现的:
8 ///
9 /// 1. 首先将所有待排序的数字放入工作列表中。
10 /// 2. 从列表的第一个数字到倒数第二个数字,逐个检查:若某一位上的数字大于他的下一位,则将它与它的下一位交换。
11 /// 3. 重复2号步骤(倒数的数字加1。例如:第一次到倒数第二个数字,第二次到倒数第三个数字,依此类推...),直至再也不能交换。
12 ///
13 /// 平均时间复杂度:O(n^2)
14 /// Stability:Yes
15 /// </summary>
16 public class BubbleSortAlgorithm
17 {
18 public static void BubbleSort<T>(IList<T> szArray) where T:IComparable
19 {
20 int i;
21 int j;
22 Http://www.clobu.com
23
24 T temp; //交换变量
25 bool swapped = false;
26 for (i = szArray.Count - 1; i >= 0; i--)
27 {
28 for (j = 1; j <= i; j++)
29 {
30 if (szArray[j - 1].CompareTo(szArray[j]) > 0)
31 {
32 temp = szArray[j - 1];
33 szArray[j - 1] = szArray[j];
34 szArray[j] = temp;
35 swapped = true;
36 }
37 }
38 if (!swapped)
39 break;
40 }
41 }
42 }
43 }
44

 

posted @ 2009-12-19 17:26 smallnest 阅读(9) | 评论 (0)编辑

最近研究算法,把常用的算法用C#实现了。整理了一下列在这里,以备查找。

  • 排序算法

交换排序

冒泡排序
快速排序
奇偶排序
鸡尾酒排序
Gnome Sort
Comb Sort

插入排序

      插入排序
      希尔排序

选择排序
      选择排序
      堆排序

合并排序
      合并排序
      Strand排序

 

  • 搜索算法

二分搜索
Interpolation搜索
jump搜索
非线性搜索
快速选择算法
ternary选择算法
Uniform二分搜索

  • 混淆(洗牌算法)

KnuthShuffle算法
Sattolo算法

posted @ 2009-12-19 17:24 smallnest 阅读(66) | 评论 (0)编辑

2009年12月15日

转载请保持文章原出处http://www.colobu.com

6.    VsVim

http://blogs.msdn.com/jaredpar/archive/2009/09/09/vim-emulator-editor-extension-released.aspx

你是一个VIM的狂热分子吗?网络不乏这些忠实的信众,如迷春哥狂热的坚持使用vim做自己的开发。这个插件为VS提供了一个VIM模拟器。你可以使用”VIM”编辑你的代码。


7.    VS 2008 File Finder

http://www.huffs.us/blogEngine/page/VS-2008-File-Finder.aspx

在一个包含N个project的解决方案中查找一个文件时,使用这个插件相当有用。它提供一个窗口,可以快速的找到项目中的某个文件。类似EclipseCtrl+Shift+R快捷键。


8.    PowerCommands for Visual Studio 2008

http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=PowerCommands&ReleaseId=559

为VS提供了一堆的命令扩展。


9.    TracExplorer

http://tracexplorer.devjavu.com/

https://groups.google.com/group/tracexplorer?pli=1

tracexplorer是一个Trac客户端。你可以在VS中浏览和管理Trac中的ticket


10.    Clone Detective for Visual Studio

http://clonedetectivevs.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=16114

此插件可以分析项目中的代码,找到那些重复的代码。重复的代码不但冗余,而且一旦修改业务逻辑,多处代码也不好维护一致性。


转载请注明:来自鸟窝

posted @ 2009-12-15 19:16 smallnest 阅读(6033) | 评论 (29)编辑