Common Data Structure Algorithms

Source: Internet
Author: User
// Tower void Hanoi (int n, string a, string B, string c) {If (n = 1) cout <A <"->" <C <Endl; else {Hanoi (n-1, A, C, B ); cout <A <"->" <C <Endl; Hanoi (n-1, B, A, c );}} // recursively implement int maxnum (INT num [], int N) {if (1 = N) return num [0]; else {int TEM = maxnum (Num, n-1); Return (TEM> num [n-1])? TEM: num [n-1] ;}} int addnum (INT num [], int N) {if (1 = N) return num [0]; else {return num [n-1] + addnum (Num, n-1) ;}} int AV (INT num [], int N) {if (1 = N) return num [0]; else {return (n-1) * AV (Num, n-1) + num [n-1])/n ;}} void Pai (INT num [], int M, int N) {Int J, TEM; If (0 = m) {for (j = 0; j <n; ++ J) {cout <num [J] <";}cout <Endl ;}else {for (j = 0; j <= m; ++ J) {TEM = num [m]; num [m] = num [J]; num [J] = TEM; Pai (Num, M-1, n); TEM = Num [m]; num [m] = num [J]; num [J] = TEM ;}} void getnext (char * P, int * Next) {Int J, k; next [0] =-1; j = 0; k =-1; int b1 = strlen (p); While (j <strlen (P)-1) {If (k =-1 | P [J] = P [k]) {++ J; ++ K; next [J] = K ;} elsek = next [k] ;}} int kemmatch (char * t, char * P) {int * Next = new int (strlen (p); getnext (p, next); int I = 0; Int J = 0; while (I <strlen (t )) {If (j =-1 | T [I] = P [J]) {++ I; ++ J ;} else {J = next [J];} If (j = ST Rlen (p) return I-strlen (p);} return-1 ;} /*************************************** * ***************** // heap insertion void constructheap (int A [], int N, int value) {A [n] = value; Int J, temp = value; j = (n-1)/2; while (j> = 0 & n! = 0) {if (a [J] <= temp) break; A [n] = A [J]; n = J; j = (n-1)/2 ;} A [n] = temp;} // heap update void updataheap (int A [], int index, int N) {Int J, temp = A [Index]; j = 2 * index + 1; while (j <n) {If (J + 1 <n & A [J + 1] <A [J]) + + J; if (A [J]> = temp) break; A [Index] = A [J]; Index = J; j = Index * 2 + 1 ;} A [Index] = temp;} void heapdelete (int A [], int N) {swap (A [0], a [n-1]); updataheap (A, 0, n-1);} // array heap void makeheap (int A [], int N) {for (INT I = N/ 2-1; I> = 0; -- I) {updataheap (A, I, n) ;}// Heid heapsort (int A [], int N) {for (INT I = n-1; I> = 1; -- I) {swap (A [0], a [I]); updataheap (A, 0, I );}} /********************************* // * specify an array. A [n], we want to construct the array B [N], where B [J] = A [0] * A [1]… A [N-1]/A [J], division is not allowed in the construction process: requires O (1) space complexity and O (n) time complexity; in addition to traversing the counter and a [n] B [N], new variables (including stack temporary variables, heap space, and Global static variables) cannot be used ); */void getb (int A [], int B [], int N) {B [0] = 1; int I; for (I = 1; I <N; + + I) {B [I] = B [I-1] * A [I-1];} for (I = n-1; I> = 1; -- I) {B [I] * = B [0]; B [0] * = A [I];} /*************************************// /bubble sort void bubblesort (int A [], int N) {int I, j; int flag = true; for (I = 0; I <n; ++ I) {flag = false; For (j = 1; j <n-I; + + J) {if (a [J-1]> A [J]) Swap (A [J-1], a [J]); flag = true;} If (! Flag) break ;}} /*************************************// /Insert the sorted void insertsort (int A [], int N) {int I, j, temp; for (I = 1; I <n; ++ I) {if (a [I] <A [I-1]) {temp = A [I]; j = I; do {A [J] = A [J-1]; -- J ;} while (j> = 1 & temp <A [J-1]); A [J] = temp ;}}} /*************************************// /hill sort void shellsort (int A [], int N) {int I, j, temp, gap; Gap = N; do {Gap = gap/3 + 1; for (I = gap; I <N; ++ I) {if (a [I] <A [I-Gap]) {temp = A [I]; j = I; do {A [J] = A [J-Gap]; j-= gap ;} while (j> = gap & temp <A [J-Gap]); A [J] = temp ;}} while (GAP> 1 );} /*************************************// /fast sort void quiksort (int A [], int L, int R) {If (L <r) {int I = L, j = r, TEM = A [l]; while (I <j) {While (I <J & A [J]> = TEM) -- J; if (I <j) A [I ++] = A [J]; while (I <J & A [I] <= TEM) ++ I; if (I <j) A [j --] = A [I];} A [I] = TEM; quiksort (A, L, I-1); quiksort (A, I + 1, R );}}/******* ***************************** // Select the sort void selectsort (int [], int N) {int I, j, k; for (I = 0; I <n-1; ++ I) {k = I; for (j = I + 1; j <n; ++ J) {if (a [J] <A [k]) k = J;} if (I! = K) Swap (A [I], a [k]) ;}} /*************************************// /Fibonacci series long maid (int n) {If (0 = N) return 0; else if (1 = N) return 1; else if (n> 1) return fig (n-1) + fig (n-2 ); else return-1;} Long fab2 (INT in) {int x = 0, y = 1, I; for (I = 2; I <= in; I ++) {Y = x + y; X = Y-X;} return y ;}; /************************************/// given integer sum, search for all combinations that meet x + y = sum in the out-of-order array void sumn (int A [], int N, int value) {qui Ksort (A, 0, n-1); int I = 0, j = n-1; while (I <j) {If (A [I] + A [J]) = value) {cout <A [I] <"+" <A [J] <Endl; ++ I; -- J ;} else if (A [I] + A [J]) <value) + + I; else -- J ;}} /************************************/// find the maximum number of K in the array // heap inserts void constructheap (int A [], int N, int value) {A [n] = value; Int J, temp = value; j = (n-1)/2; while (j> = 0 & n! = 0) {if (a [J] <= temp) break; A [n] = A [J]; n = J; j = (n-1)/2 ;} A [n] = temp;} // heap update void updataheap (int A [], int index, int N) {Int J, temp = A [Index]; j = 2 * index + 1; while (j <n) {If (J + 1 <n & A [J + 1] <A [J]) + + J; if (A [J]> = temp) break; A [Index] = A [J]; Index = J; j = Index * 2 + 1 ;} A [Index] = temp;} void maxk (int A [], int N, int K) {int I; int * temp = new int [k]; for (I = 0; I <n; ++ I) {if (I <k) constructheap (temp, I, a [I]); // first construct K heap else {If (temp [0] <A [I]) {temp [0] = A [I]; updataheap (temp, 0, k ); // update the K heap. }}for (I = 0; I <K; ++ I) {cout <temp [I] <"";} delete [] temp ;} /************************************/

Common Data Structure Algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.