07 2012 档案

摘要:HeapSort is an sort algorithms, using heap tree(Full Binary Tree) for help.Implementing this sort should execute three steps1. Adjust an entire array to a heap tree(big heap tree for ascending, small heap tree for descending sort)2. Get the root of the heap tree arr[0] and swap it with last one of . 阅读全文
posted @ 2012-07-18 18:07 EricWen 阅读(216) 评论(0) 推荐(0)
摘要:A deck pokers have 52 pieces except small king and big king, which with four different suit. We want to generate 52 pieces of pokers and sort them by suit first or by value first.We make a rule that isSpade>Heart>Diamond>Club, assign values (4,3,2,1) to each of them.And anthter rule is code 阅读全文
posted @ 2012-07-17 01:04 EricWen 阅读(454) 评论(0) 推荐(0)
摘要:归并排序简言之,假设一个数组分成两个有同序的小数组(1/2组),然后将二者合并。递归的假设,将小组再假设分成两个有同序的小组(1/4组),然后合并二者。递归。。。。最后1/n组剩下一个数据了,两个1/n组合并。这应该easy哈。递归实现如下: ///<summary>///MergeSortO(nlogn)///</summary>///<paramname="arr"></param>publicstaticvoidMergeSort(int[]arr){if(arr==null){thrownewArgumentNullE 阅读全文
posted @ 2012-07-16 18:32 EricWen 阅读(191) 评论(0) 推荐(0)
摘要:希尔排序的一种缩小增量(Incremental)的排序,是直接插入排序的一种升级。基本思想就是,将原有的数组arr,分成Incremental 个小数组,然后对各个小数组进行插入排序,当然我认为,也可以用其他排序算法。然后将增量(Incremental)减半,再分组,再排序。直到最后Incremental=1,也必须等于1,这时,就只能分成一组了,相当于对整个数组进行一次插入排序。这样做的好处,在于,不断细分数组,使得在排序过程中减少了数据的移动量。我的C#实现如下:///<summary>///ShellsortO((nlogn)^2)///</summary>/// 阅读全文
posted @ 2012-07-16 16:01 EricWen 阅读(449) 评论(0) 推荐(0)
摘要:写一个方法得到一个素数数组,这些素数不能大于给定的自然数。我看网上大多数的实现都是用自然数n除以2到n/2+1的数,如果整除了,就判定不是素数。我的想法不一样,我一个数组保存已经得到的素数,然后用n除以这些素数,如果整除了,就判定不是素数。具体实现如下:staticint[]GetPrimeNumbers(intboundary){List<int>primeList=newList<int>();intn=2;while(n<=boundary){boolisPrime=true;for(inti=0;i<primeList.Count;i++){if(n 阅读全文
posted @ 2012-07-15 02:12 EricWen 阅读(2286) 评论(0) 推荐(0)
摘要:关于排序的一些常识可以问百科,以下是用C#实现的排序算法。1.冒泡排序 BubbleSort///<summary>///BubblesortO(n^2)///</summary>///<paramname="arr"></param>publicstaticvoidBubbleSort(int[]arr){if(arr==null){thrownewArgumentNullException();}for(inti=0;i<arr.Length-1;i++){for(intj=arr.Length-1;j>i;j 阅读全文
posted @ 2012-07-15 01:54 EricWen 阅读(230) 评论(0) 推荐(0)
摘要:斐波那契数列(1,1,2,3,5,8,...)用函数表示为f(n)=f(n-1)+f(n-2) (n>2,f(1)=1,f(2)=1)首先一般想到递归算法://////UserecursivemethodtoimplementFibonacci/////////staticintFn(intn){if(n46memorywilloverflow}递归算法时间复杂度是O(n2), 空间复杂度也很高的。当然不是最优的。自然我们想到了非递归算法了。一般的实现如下://////UsethreevariablestoimplementFibonacci/////////staticintFn1(i 阅读全文
posted @ 2012-07-12 23:56 EricWen 阅读(16479) 评论(1) 推荐(0)