Algorithms
PYPARA
Things change ,roll with it.
展开
-
Stack
简单版 Stack Stack.h#pragma oncetemplate<class T>class stack {public: stack(); ~stack(); bool empty(); T pop(); bool push(T);private: T* data; int top;};Stack.cpp#include"St原创 2017-02-16 17:08:43 · 302 阅读 · 0 评论 -
Randomized quickSort
C/C++中产生随机数(rand,srand用法) http://blog.csdn.net/PYPARA/article/details/54773968 Quick sort http://blog.csdn.net/pypara/article/details/54773861C++:int partition(int *a, int lhs, int rhs);int原创 2017-01-29 21:38:16 · 502 阅读 · 0 评论 -
Quick sort
PARTITION(A, p, q)⊳A[p. . q] x←A[p] ⊳pivot= A[p] i←p forj←p+ 1 toq do ifA[j] ≤x theni←i+ 1 exchange A[i] ↔A[j] exchange A[p] ↔A[i] return i原创 2017-01-29 20:36:25 · 253 阅读 · 0 评论 -
Fibonacci number
Recursive definition:Fn=0 if n= 0; =1 if n= 1; =Fn–1+ Fn–2 if n≥2. Naive recursive algorithm:Ω(φn) (exponential time), where φ=is the golden ratio Bottom-up: •Compute原创 2017-01-29 15:52:18 · 336 阅读 · 0 评论 -
Powering a number
Powering a numberProblem:Compute an, where n ∈N.Naive algorithm:Θ(n).an=an/2 ⋅an/2 if n is even; =a(n–1)/2 ⋅a(n–1)/2 ⋅a if n is odd. Divide-and-conquer algorithm:T(n) = T(n/2原创 2017-01-29 15:26:55 · 344 阅读 · 0 评论 -
BinarySearch
/*Find an element in a sorted array:1.Divide:Check middle element.2.Conquer:Recursively search 1subarray.3.Combine:Trivial.*/C++:bool BinarySearch(int* a,int lhs,int rhs,int val) { if (lhs > r原创 2017-01-29 00:00:28 · 223 阅读 · 0 评论 -
Merge sort
pseudocode:MERGE-SORTA[1 . . n]1.If n= 1, done.2.Recursively sort A[ 1 . . ⎡n/2⎤]and A[ ⎡n/2⎤+1 . . n ] .3.“Merge”the 2sorted lists.C++: const int N = 100;void merge(int * a,int lhs,int mid,int rhs原创 2017-01-26 14:39:24 · 205 阅读 · 0 评论 -
Insertion sort
pseudocode:INSERTION-SORT (A, n)⊳A[1 . . n] for j ←2 to n do key ←A[ j] i ←j –1 while i > 0 and A[i] > key do A[i+1] ←A[i] i原创 2017-01-24 22:16:22 · 227 阅读 · 0 评论 -
CountingSort
/*for i←1 to k do C[i] ←0for j←1ton do C[A[j]] ←C[A[j]] + 1 ⊳C[i] = |{key = i}|for i←2 to k doC[i] ←C[i] + C[i–1] ⊳C[i] = |{key ≤i}|for j←n downto 1 do B[C[A[j原创 2017-02-13 16:21:37 · 354 阅读 · 0 评论 -
List(double linked)
简易版链表 ,目前还有些问题, 1.由于构造函数中会new一个Node,所以链表中会对出一个由默认T()构造的Node 2.打印链表中的元素非常不方便 等/*LIST-SEARCH(L,k)x=L.headwhile x!=NIL and key !=k x=x.nextreturn xLIST-INSERT(L,x)x.next= L.headif L.head!=NIL原创 2017-02-21 18:23:13 · 993 阅读 · 0 评论 -
Queue
Queue.h#pragma oncetemplate<class T>class queue {public: queue(); ~queue(); bool enqueue(const T); T dequeue(); bool empty() const; bool full() const;private: T* data;原创 2017-02-16 20:18:39 · 278 阅读 · 0 评论 -
HeapSort
MAX-HEAPIFY (A,i)1. l=LEFT(i)2. r=RIGHT(i)3. if l <= A.heap-size and A[l]>A[i]4. largest=l5. else largest=i6. if r <= A.heap-size and A[r]>A[largest]7. largest=r8. if largest!=i9.原创 2017-02-01 14:52:12 · 223 阅读 · 0 评论