// Assuming array ranges from [0..arraySize-1] GetFromOrderedArray(0,arraySize-1) . . void GetFromOrderedArray(int lowBound,int highBound) { if (hi < low) return; middlePos = lowBound+(highBound-lowBound)/2 // middlePos is now at the element in the middle // between lowBound and highBound, so we just add // it to the tree AddElement ( theOrderedArray[middlePos] ) // Pick the middle one "to the left" AddFromOrderedArray(lowBound,middlePos-1) // Pick the middle one "to the right" AddFromOrderedArray(middlePos+1,highBound) }
void RemoveElement(TreeElement* theOneToRemove) { TreeElement* pParent = theOneToRemove->GetParent(); // Ok, so it has a parent, then we'll simply just disconnect it. if (pParent) { if (pParent->GetLeftChild() == theOneToRemove) { pParent->SetLeftChild(NULL); } else { ASSERT(pParent->GetRightChild() == theOneToRemove); pParent->SetRightChild(NULL); } } else { // No parent? Then we're removing the root element. theTopMostElement = NULL; } // Disconnected, now we reconnect its children (if any) // just by adding them as we add any other node. if (theOneToRemove->GetLeftChild()) AddElement(theOneToRemove->GetLeftChild()); if (theOneToRemove->GetRightChild()) AddElement(theOneToRemove->GetRightChild()); //Zap the element (if that's what you want to do) delete theOneToRemove; }
]]>Least Common Mutiplehttp://www.shnenglu.com/zytoftuncun/archive/2007/06/03/25369.htmlAIBPXTSHMFAIBPXTSHMFSun, 03 Jun 2007 03:44:00 GMThttp://www.shnenglu.com/zytoftuncun/archive/2007/06/03/25369.htmlhttp://www.shnenglu.com/zytoftuncun/comments/25369.htmlhttp://www.shnenglu.com/zytoftuncun/archive/2007/06/03/25369.html#Feedback0http://www.shnenglu.com/zytoftuncun/comments/commentRss/25369.htmlhttp://www.shnenglu.com/zytoftuncun/services/trackbacks/25369.html//闈為掑綊杈楄漿鐩擱櫎姹傛渶澶у叕綰︽暟 int gcd(int a,int b) { int r=0; r=a%b; while(r) { a=b; b=r; r=a%b; } return b; } //鏈灝忓叕鍊嶆暟 int lcm(int a,int b) { return (a*b)?a*b/gcd(a,b):0; }
]]>Greatest Common Divisorhttp://www.shnenglu.com/zytoftuncun/archive/2007/06/02/25352.htmlAIBPXTSHMFAIBPXTSHMFSat, 02 Jun 2007 11:59:00 GMThttp://www.shnenglu.com/zytoftuncun/archive/2007/06/02/25352.htmlhttp://www.shnenglu.com/zytoftuncun/comments/25352.htmlhttp://www.shnenglu.com/zytoftuncun/archive/2007/06/02/25352.html#Feedback0http://www.shnenglu.com/zytoftuncun/comments/commentRss/25352.htmlhttp://www.shnenglu.com/zytoftuncun/services/trackbacks/25352.html
//姹傛渶澶у叕綰︽暟,鍏紡if(a=b*q+r)then(gcd(a,b)=gcd(b,r)) int gcd(int a,int b) { return (a%b)?gcd(b,a%b):b; }
闈為掑綊綆楁硶:
//闈為掑綊杈楄漿鐩擱櫎 int gcd(int a,int b) { int r=0; r=a%b; while(r) { a=b; b=r; r=a%b; } return b; }
int main(){ 聽聽聽 int b[6]={11,65,53,78,38,63}; 聽聽聽 mergesort(b,0,5); 聽for(int i=0;i<6;++i) 聽 cout<<b[i]<<'\t'; 聽return(0); }
浠ヤ笅寮曠敤鑷細gooler鐨勪笓鏍?a >http://blog.csdn.net/Gooler/archive/2006/03/22/632422.aspx鏈夊悇縐嶆帓搴忕畻娉晀uicksort,heapsort... /* 聽* A is an array and p, q, and r are indices numbering elements of the array 聽* such that p <= q < r. 聽* 聽* This procedure assumes that the subarrays A[p. .q] and A[q + 1. .r] are in 聽* sorted order. It merges them to form a single sorted subarray that replaces 聽* the current subarray A[p. .r]. 聽*/ void merge(int A[], int p, int q, int r) { 聽聽聽 int i, j, k, size; 聽聽聽 int* B; 聽聽聽 聽聽聽 size = r - p + 1;
聽聽聽 B = new int[size]; /* temp arry for storing the merge result */ 聽聽聽 聽聽聽 /* initialize B */ 聽聽聽 for (i = 0; i < size; ++i) { 聽聽聽 聽聽聽 B[i] = 0; 聽聽聽 }
聽聽聽 i = p; 聽聽聽 j = q + 1; 聽聽聽 k = 0; 聽聽聽 聽聽聽 /* compare and copy the smaller to B */ 聽聽聽 while (i <= q && j <= r) { 聽聽聽 聽聽聽 if (A[i] < A[j]) { 聽聽聽 聽聽聽 聽聽聽 B[k++] = A[i++]; 聽聽聽 聽聽聽 } else { 聽聽聽 聽聽聽 聽聽聽 B[k++] = A[j++]; 聽聽聽 聽聽聽 } 聽聽聽 } 聽聽聽 聽聽聽 /* copy the rest to B */ 聽聽聽 while (i <= q) { 聽聽聽 聽聽聽 B[k++] = A[i++]; 聽聽聽 }聽聽聽 聽聽聽 while (j <= r) { 聽聽聽 聽聽聽 B[k++] = A[j++]; 聽聽聽 } 聽聽聽 聽聽聽 /* replace A[p..r] with B[0..r-p] */ 聽聽聽 for (i = p, k = 0; i <= r; ++i, ++k) { 聽聽聽 聽聽聽 A[i] = B[k]; 聽聽聽 }
聽聽聽 delete[] B; }
/* 聽* This procedure sorts the elements in the subarray A[p. .r]. 聽* 聽* If p >= r, the subarray has at most one element and is therefore 聽* already sorted. Otherwise, the divide step simply computes an index 聽* q that partitions A[p. .r] into two subarrays: A[p. .q], containing n/2 聽* elements, and A[q + 1. .r], containing n/2 elements. 聽*/ void mergeSort(int A[], int p, int r) { 聽聽聽 if (p >= r) return; 聽聽聽 聽聽聽 int q = (p + r) / 2; 聽聽聽 聽聽聽 mergeSort(A, p, q); 聽聽聽 mergeSort(A, q + 1, r); 聽聽聽 merge(A, p, q, r); }