锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
榪欓噷緇欏嚭SGI STL鍐呭瓨鍒嗛厤鍣ㄧ殑涓涓畝鍗曞疄鐜幫細
棣栧厛瀹氫箟涓涓猽nion鏁版嵁緇撴瀯錛?br />
2 union obj* free_list_link;
3 char client_data[1];
4 };
榪欎釜union緇撴瀯浣撶殑鏈澶уぇ灝忎負4bytes 錛堝湪32bits 騫沖彴涓婏級錛?bytes 錛堝湪64bits騫沖彴涓婏級銆?br />
鍋囪閭g墖鍐呭瓨鐨勫湴鍧涓篶hunk錛岄偅涔堟垜浠彲浠ヨ繖鏍峰仛錛?
2 next_obj = (obj*)chunk;
3 for (int i = 0; ; i++) {
4 current_obj = next_obj;
5 next_obj = (obj*)((char*)next_obj + m);
6 if (n - 1 == i) {
7 current_obj -> free_list_link = 0;
8 break;
9 } else {
10 current_obj -> free_list_link = next_obj;
11 }
12 }
13
]]>
2 {
3 int tmp = 0;
4 for (int i = 1; i < n; i++) {
5 tmp += 2*(i-1)+1;
6 if (tmp == n)
7 return i;
8 continue;
9 }
10 if (n!=0) {
11 printf("no integer sqare found!\n");
12 tmp = -1;
13 }
14 return tmp;
15 }
16
]]>
2 {
3 static int n = 1;
4 printf("%d:", n++);
5 printf("[");
6 for (int i = 0; i < s.size(); i++)
7 printf(" %d ", s[i]);
8 printf("]\n");
9 }
10
11 void permutation(std::vector<int>& v, int beg, int end)
12 {
13 if (beg > end) {
14 print(v);
15 return;
16 }
17 for (int i = beg; i <= end; i++) {
18 std::swap(v[i], v[beg]);
19 permutation(v, beg+1, end); // pleate note, here always beg+1, not i+1
20 std::swap(v[i], v[beg]);
21 }
22 }
23
24 void pm(std::vector<int>& v)
25 {
26 std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
27 printf("\nfull permulation are:\n");
28 permutation(v, 0, v.size()-1);
29 }
30
31 void print(const std::vector<int>& v, int beg, int end)
32 {
33 static int n = 1;
34 printf("%d:", n++);
35 printf("[");
36 std::copy(v.begin()+beg, v.begin()+end+1, std::ostream_iterator<int>(std::cout, " "));
37 printf("]\n");
38 }
39
40 void fullcombination(std::vector<int>& v)
41 {
42 printf("full combination are:\n");
43 for (unsigned int i = 0; i < v.size(); i++) {
44 print(v, i, i);
45 for (unsigned int j = i+1; j < v.size(); j++) {
46 for (int k = 0; k < v.size()-j; k++) {
47 std::swap(v[j+k], v[j]);
48 print(v, i, j);
49 std::swap(v[j+k], v[j]);
50 }
51 }
52 }
53 }
54
]]>



permute(i)
if i == N output A[N]
else
for j = i to N do
swap(A[i], A[j])
permute(i+1)
swap(A[i], A[j])
]]>
2 #include <iterator>
3 #include <algorithm>
4 #include <iostream>
5 #include <cstdlib>
6 #include <ctime>
7 #include <cassert>
8
9 void printv(const std::vector<int>& v)
10 {
11 std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
12 std::cout << std::endl;
13 }
14
15 void printv(const std::vector<int>& v, int l, int h)
16 {
17 std::copy(v.begin()+l, v.begin()+h+1, std::ostream_iterator<int>(std::cout, " "));
18 std::cout << std::endl;
19 }
20
21 int exchange(int& a, int& b)
22 {
23 int tmp = a;
24 a = b;
25 b = tmp;
26 }
27
28 int partition1(std::vector<int>& v, int l, int h)
29 {
30 int i = l-1;
31 int pivot = v[h];
32 int j = l;
33 while (j < h) {
34 if (v[j] < pivot) {
35 i++;
36 exchange(v[i], v[j]);
37 }
38 j++;
39 }
40 exchange(v[i+1], v[h]);
41 return i+1;
42 }
43
44 int partition2(std::vector<int>& v, int l, int h)
45 {
46 int m = l+(h-l)/2;
47 int i = l;
48 int j = h;
49
50 int pivot = v[m];
51 while (1) {
52 while (v[i] < pivot) i++;
53 while (v[j] > pivot) j--;
54 if (!(i<j)) {
57 return j;
58 }
59 exchange(v[i], v[j]);
60 i++;j--;
61 }
62 }
63
64 void myQuickSort1(std::vector<int>& v, int l, int h)
65 {
66 /*
67 Please note, returned m is the loc of pivot,
68 in below myQuickSort1, can remove m, since left of m, all <= pivot
69 right of m, all >= pivot
70 */
71 if (l < h) {
72 int m = partition1(v, l, h);
73 myQuickSort1(v, l, m-1);
74 myQuickSort1(v, m+1, h);
75 }
76 }
77
78 void myQuickSort2(std::vector<int>& v, int l, int h)
79 {
80 /*
81 if partition2 return i, please call below
82 myQuickSort2(v, l, m-1);
83 myQuickSort2(v, m, h);
84 else if it return j, please call below
85 myQuickSort2(v, l, m);
86 myQuickSort2(v, m+1, h);
87 */
88 if (l < h) {
89 int m = partition2(v, l, h);
90 myQuickSort2(v, l, m);
91 myQuickSort2(v, m+1, h);
92 }
93 }
94
95 int main()
96 {
97
98 const int N = 100;
99 srand(time(0));
100 std::vector<int> v, vtmp;
101 for (int i = 0; i < N; i++)
102 v.push_back(int(rand()%N));
103 vtmp.insert(vtmp.end(), v.begin(), v.end());
104 std::cout << "Original data sequence is:\n";
105 std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
106 std::cout << std::endl;
107 myQuickSort1(v, 0, N-1);
108 std::cout << "After quicksort(1), the data sequence is:\n";
109 std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
110 std::cout << std::endl;
111
112 myQuickSort2(vtmp, 0, N-1);
113 std::cout << "After quicksort(2), the data sequence is:\n";
114 std::copy(vtmp.begin(), vtmp.end(), std::ostream_iterator<int>(std::cout, " "));
115 std::cout << std::endl;
116
117 assert(v==vtmp);
118 return 0;
119 }
120
]]>
2 input: an unordered array a of length count
3
4 (first place a in max-heap order)
5 heapify(a, count)
6
7 end := count-1 //in languages with zero-based arrays the children are 2*i+1 and 2*i+2
8 while end > 0 do
9 (swap the root(maximum value) of the heap with the last element of the heap)
10 swap(a[end], a[0])
11 (put the heap back in max-heap order)
12 siftDown(a, 0, end-1)
13 (decrease the size of the heap by one so that the previous max value will
14 stay in its proper placement)
15 end := end - 1
16
17 function heapify(a, count) is
18 (start is assigned the index in a of the last parent node)
19 start := count / 2 - 1
20
21 while start ≥ 0 do
22 (sift down the node at index start to the proper place such that all nodes below
23 the start index are in heap order)
24 siftDown(a, start, count-1)
25 start := start - 1
26 (after sifting down the root all nodes/elements are in heap order)
27
28 function siftDown(a, start, end) is
29 input: end represents the limit of how far down the heap
30 to sift.
31 root := start
32
33 while root * 2 + 1 ≤ end do (While the root has at least one child)
34 child := root * 2 + 1 (root*2 + 1 points to the left child)
35 swap := root (keeps track of child to swap with)
36 (check if root is smaller than left child)
37 if a[swap] < a[child]
38 swap := child
39 (check if right child exists, and if it's bigger than what we're currently swapping with)
40 if child < end and a[swap] < a[child+1]
41 swap := child + 1
42 (check if we need to swap at all)
43 if swap != root
44 swap(a[root], a[swap])
45 root := swap (repeat to continue sifting down the child now)
46 else
47 return
48
C++ 瀹炵幇
2 template <typename T>
3 void heapsort(std::vector<T>& a)
4 {
5 int sz = (int)a.size();
6 for (int i = sz/2; i >= 0; i--)
7 percDown(a, i, sz);
8 std::cout << "build heap is done.\n";
9 for (int j = sz-1; j > 0; j--) {
10 std::swap(a[0], a[j]);
11 percDown(a, 0, j);
12 }
13 }
14
15 inline int leftChild(int i)
16 {
17 return 2*i+1;
18 }
19
20 template <typename T>
21 void percDown(std::vector<T>& a, int i, int n)
22 {
23 int child;
24 T tmp;
25 for (tmp = a[i]; leftChild(i) < n; i = child) {
26 child = leftChild(i);
27 if (child != n-1 && a[child] < a[child+1])
28 child++;
29 if (tmp < a[child])
30 a[i] = a[child];
31 else
32 break;
33 }
34 a[i] = tmp;
35 }
36
]]>
2 {
3 char* i, *j;
4 if (s && *s!='\0') {
5 i = s; j = s+1;
6 while (*j!='\0') {
7 if (*i!=*j)
8 *++i = *j;
9 j++;
10 }
11 *++i = '\0';
12 }
13 }
14
]]>
2 pivotValue := list[pivotIndex]
3 swap list[pivotIndex] and list[right] // Move pivot to end
4 storeIndex := left
5 for i from left to right-1
6 if list[i] < pivotValue
7 swap list[storeIndex] and list[i]
8 storeIndex := storeIndex + 1
9 swap list[right] and list[storeIndex] // Move pivot to its final place
10 return storeIndex
2. selection algorithm
2 loop
3 select pivotIndex between left and right
4 pivotNewIndex := partition(list, left, right, pivotIndex)
5 if k = pivotNewIndex - left + 1
6 return list[pivotNewIndex]
7 else if k < pivotNewIndex - left + 1
8 right := pivotNewIndex-1
9 else
10 left := pivotNewIndex+1
11 k := k - pivotNewIndex
>> In above last statement, one bug. Should be
k := k-pivotNewIndex-left+1
left := pivotNewIndex+1
3. code
2 {
3 int i = l, j = l;
4 int p = a[r];
5 while (j<r) {
6 if (a[j] < p) {
7 std::swap(a[i], a[j]);
8 i++;
9 }
10 j++;
11 }
12 std::swap(a[r], a[i]);
13 return i;
14 }
15
16 void select_kth_smallest(std::vector<int>& a, int l, int r, int k)
17 {
18 while (1) {
19 int cut = partition(a, l, r);
20 #if 0
21 std::copy(a.begin()+l, a.begin()+cut, std::ostream_iterator<int>(std::cout, " "));
22 std::cout << " <=" << a[cut] << "=> ";
23 std::copy(a.begin()+cut+1, a.begin()+r+1, std::ostream_iterator<int>(std::cout, " "));
24 std::cout << "cut("<<cut<<"),k("<<k<<"),l("<<l<<"),r("<<r<<")\n";
25 #endif
26 if (cut-l+1==k)
27 break;
28 else if (k < cut-l+1)
29 r = cut-1;
30 else {
31 k = k-cut-1+l;
32 l = cut+1;
33 }
34 }
35 }
36
]]>
2 int m_nKey;
3 ListNode* m_pNext;
4 };
鐞嗚В棰樼洰鐨勬剰鎬濓紝鍙渶瑕佸弽杞緭鍑烘瘡涓嚑鐐圭殑鍊煎氨鍙互浜嗐傚茍涓嶉渶瑕佸皢鍗曢摼琛ㄨ繘琛屽弽杞?br>
2 {
3 if (*phead!=NULL) {
4 printStringListReversely(phead->m_pNext);
5 print("%d ", phead->m_nKey);
6 }
7 }
寤朵幾錛?) 浠庡熬鍒板ご杈撳嚭瀛楃涓? 2錛夊畾涔変釜鍑芥暟姹傚瓧絎︿覆鐨勯暱搴︼紝鍑芥暟鍐呬笉鑳藉0鏄庝換浣曞彉閲忋?br>
]]>
2 int M = 1000; // number range
3 std::vector<int> a(N); // can be imported from external file number by number
4 for (int i = 0; i < N; i++)
5 a[i] = (int)rand()%M;
6 std::copy(a.begin(), a.end(), std::ostream_iterator<int>(std::cout, " "));
7 std::cout << "\n";
8 // bit map setup for existence of each number
9 unsigned int nbytes = M%8 ? (M/8+1) : (M/8);
10 std::cout << "nbytes = " << nbytes << "\n";
11
12 char* p = new char [nbytes];
13 memset(p, 0, sizeof(char)*nbytes);
14
15 for (int i = 0; i < N; i++) {
16 unsigned int index = a[i]/8;
17 unsigned int bitpos = a[i]%8;
18 char* tmp = p+index;
19 *tmp |= 1 << bitpos;
20 //std::cout << "bit pos set to 1 : " << 8*index+bitpos << "\n";
21 }
22 for (int i = nbytes-1; i >= 0; i--) {
23 printf("%02X ", (char)*(p+i)&0xFF);
24 }
25 std::cout << "\n";
26 delete [] p;
27
]]>
2 void displayHexBin(const T& v)
3 {
4 const unsigned char c2h[] = "0123456789ABCDEF";
5 const unsigned char c2b[] = "01";
6
7 unsigned char* p = (unsigned char*)&v;
8 char* buf = new char [sizeof(T)*2+1];
9 char* ptmp = buf;
10 p = p + sizeof(T)-1;
11 for (int i = 0; i < sizeof(T); i++, --p) {
12 *buf++ = c2h[*p >> 4];
13 *buf++ = c2h[*p & 0x0F];
14 }
15 *buf = '\0';
16 printf("hex format displayed as %s\n", ptmp);
17
18 delete [] ptmp;
19 p = (unsigned char*)&v; p = p + sizeof(T)-1;
20 ptmp = buf = new char [sizeof(T)*8+1];
21 for (int i = 0; i < sizeof(T); i++, --p) {
22 for (int j = 0; j < 8; j++)
23 *buf++ = c2b[(*p >> (7-j)) & 0x1];
24 }
25 *buf = '\0';
26 printf("bin format displayed as %s\n", ptmp);
27 delete [] ptmp;
28 }
29
]]>
2 {
3 return (n&1)==0; // n%2 is more expensive
4 }
5
6 void group_oddeven(std::vector<int>& a, bool (*func)(int))
7 {
8 int i = 0, j = a.size()-1;
9 int buf = 0;
10 while (i < j) {
11 if (!func(a[i])) // odd, move forward
12 { i++; continue; }
13 if (func(a[j])) // even, move backward
14 { j--; continue; }
15
16 std::swap(a[i++], a[j--]);
17 }
18 }
19
]]>
2 int data[N];
3 int minidx[N];
4 int top;
5 public:
6 stackmin()
7 : top(-1)
8 {
9 ::memset(minidx, -1, sizeof(int)*N);
10 ::memset(data, 0, sizeof(int)*N);
11 }
12
13 bool push(int d)
14 {
15 if (top < N-1)
16 data[++top] = d;
17 else
18 return false;
19 if (top==0)
20 minidx[top] = 0;
21 else if (data[minidx[top-1]] > d)
22 minidx[top] = top;
23 else
24 minidx[top] = minidx[top-1];
25 return true;
26 }
27 bool pop(int& d)
28 {
29 if (top<0)
30 return false;
31 else
32
33 d = data[top];
34 minidx[top] = -1;
35 top--;
36 return true;
37 }
38 bool min(int& m)
39 {
40 if (top>=0)
41 m = data[minidx[top]];
42 else
43 return false;
44 return true;
45 }
46 void print()
47 {
48 for (int i = 0; i <= top; i++)
49 std::cout << data[i] << " ";
50 std::cout << "\n";
51 }
52 };
53
鍊熷姪鍙︿竴涓唴閮ㄧ殑鏁扮粍錛岀敤鏉ュ瓨鍌ㄦ瘡嬈ush鍏冪礌涔嬪悗鐨勬爤鏈灝忓肩殑绱㈠紩錛屼究鍙緢鏂逛究鐨勫湪O(1)鐨勬椂闂村唴瀹屾垚浠ヤ笂涓夌鎿嶄綔銆備竴縐嶇┖闂存崲鏃墮棿鐨勬柟娉曘?br>
]]>
2 {
3 if (n==0)
4 return 1;
5 else (n%2==0)
6 return power(x*x, n/2);
7 else
8 return x*power(x*x, n/2);
9 }
10
11
]]>
2 {
3 int m_value;
4 BSTreeNode* m_pLeft;
5 BSTreeNode* m_pRight;
6 };
7
8 BSTreeNode* convert(BSTreeNode* phead, bool asRight)
9 {
10 if (!phead)
11 return NULL;
12 BSTreeNode* pLeft = 0, *pRight=0;
13 if (phead->m_pLeft)
14 pLeft = convert(phead->m_pLeft, false);
15 if (pLeft) {
16 pLeft->m_pRight = phead;
17 phead->m_pLeft = pLeft;
18 }
19 if (phead->m_pRight)
20 pRight = convert(phead->m_pRight, true);
21 if (pRight) {
22 pRight->m_pLeft = phead;
23 phead->m_pRight = pRight;
24 }
25 BSTreeNode* ptmp = phead;
26 if (asRight) {
27 while (ptmp->m_pLeft) {
28 ptmp = ptmp->m_pLeft;
29 }
30 } else {
31 while (ptmp->m_pRight) {
32 ptmp = ptmp->m_pRight;
33 }
34 }
35 return ptmp;
36 }
37
38 BSTreeNode* convert2doublelist(BSTreeNode* phead)
39 {
40 return convert(phead, true);
41 }
42
]]>
2 {
3 assert(arr!=0);
4 int maxSum = 0;
5 int tmpSum = 0;
6 beg = 0; end = 0;
7 for (int i = 0; i < length; i++) {
8 tmpSum += arr[i];
9 if (tmpSum < 0) { // discard it
10 tmpSum = 0;
11 beg = end = i+1;
12 }
13 if (tmpSum > maxSum) {
14 maxSum = tmpSum;
15 end = i+1;
16 }
17 }
18 if (maxSum ==0 ) { // if all numbers are negative, return max value
19 maxSum = arr[0];
20 for (int i = 1; i < length; i++)
21 if (maxSum < arr[i]) {
22 maxSum = arr[i];
23 beg = i; end = i+1;
24 }
25 }
26 return maxSum;
27 }
28
]]>
2 int value;
3 listNode* next;
4 };
5
6 listNode* reverseList(listNode* phead)
7 {
8 listNode* pcur = phead;
9 listNode* pprev = 0, *pnext = 0;
10 while (pcur) {
11 pnext = pcur->next;
12 pcur->next = pprev;
13
14 pprev = pcur;
15 pcur = pnext;
16 }
17 return pcur;
18 }
]]>