Sample code (1)
decimal factorial (int n) { if (n = = 0) return 1; else return n * factorial (n-1); }
"Analysis"
Factorial (factorial), given the size n, the number of basic steps performed by the algorithm is n, so the algorithm complexity is O (n).
Sample Code (2)
int findmaxelement (int[] array) { int max = array[0]; for (int i = 0; i < array. Length; i++) { if (Array[i] > Max) { max = array[i]; } } return max; }
Analysis
Here, n is the size of an array of arrays, the worst case is to compare n times to get the maximum value, so the algorithm complexity is O (n).
Sample Code (3)
Long Findinversions (int[] array) { long inversions = 0; for (int i = 0; i < array. Length; i++) for (int j = i + 1; j < Array. Length; J + +) if (Array[i] > Array[j]) inversions++; return inversions; }
"Analysis"
Here, n is the size of an array of arrays, the number of executions of the basic steps is approximately n (n-1)/2, so the algorithm complexity is O (n2).
Sample Code (4)
Long Summn (int n, int m) { long sum = 0; for (int x = 0; x < n; + +) for (int y = 0; y < m; y++) sum + = x * y; return sum; }
Analysis
Given the size n and M, the number of executions of the basic step is n*m, so the algorithm complexity is O (n^2).
Sample Code (5)
Decimal Sum3 (int n) { decimal sum = 0; for (int a = 0, a < n; a++) for (int b = 0, b < n; b++) for (int c = 0; c < n; c + +) sum + = A * b * c; return sum; }
"Analysis"
Here, given the size n, the number of executions of the basic step is approximately n*n*n, so the algorithm complexity is O (n^3).
Sample code (6)
Decimal calculation (int n) { decimal result = 0; for (int i = 0; i < (1 << n); i++) result + = i; return result; }
Analysis
Here, given the size n, the number of executions of the basic step is 2n, so the algorithm complexity is O (2n).
Sample code (7)
Fibonacci Sequence:
Fib (0) = 0
Fib (1) = 1
FIB (n) = fib (n-1) + fib (n-2)
F () = 0, 1, 1, 2, 3, 5, 8, ...
int Fibonacci (int n) { if (n <= 1) return n; else return Fibonacci (n-1) + Fibonacci (n-2); }
Analysis
Here, given the size of N, the time required to calculate the FIB (n) is the amount of time to calculate the FIB (n-1) and the time of the calculation of the FIB (n-2).
T (n<=1) = O (1)
T (n) = t (n-1) + t (n-2) + O (1)
FIB (5)
/ \
FIB (4) fib (3)
/ \ / \
FIB (3) fib (2) fib (2) fib (1)
/ \ / \ / \ / \
The complexity of the algorithm is O (2^n) by using recursive tree structure description.
Sample code (8)
int Fibonacci (int n) { if (n <= 1) return n; else { int[] f = new Int[n + 1]; F[0] = 0; F[1] = 1; for (int i = 2; I <= n; i++) { F[i] = f[i-1] + f[i-2]; } return f[n]; } }
"Analysis"
Also, we use the array F to store the results of the Fibonacci sequence, which optimizes the complexity of the algorithm to O (n).
Sample Code (9)
int Fibonacci (int n) { if (n <= 1) return n; else { int iter1 = 0; int iter2 = 1; int f = 0; for (int i = 2; I <= n; i++) { f = iter1 + Iter2; Iter1 = Iter2; Iter2 = f; } return f; } }
Analysis
Also, because only the first two calculations are actually useful, we can use intermediate variables to store them, so we don't have to create arrays to save space. The same algorithm is optimized for the complexity of O (n).
Sample code (10)
static int Fibonacci (int n) { if (n <= 1) return n; Int[,] f = {{1, 1}, {1, 0}}; Power (f, n-1); Return f[0, 0]; } static void Power (int[,] f, int n) { if (n <= 1) return; int[,] m = {{1, 1}, {1, 0}}; Power (f, N/2); Multiply (F, f); if (n% 2! = 0) Multiply (F, m); } static void Multiply (int[,] f, int[,] m) { int x = f[0, 0] * m[0, 0] + f[0, 1] * m[1, 0]; int y = f[0, 0] * m[0, 1] + f[0, 1] * m[1, 1]; int z = f[1, 0] * m[0, 0] + f[1, 1] * m[1, 0]; int w = f[1, 0] * m[0, 1] + f[1, 1] * m[1, 1]; F[0, 0] = x; F[0, 1] = y; f[1, 0] = Z; F[1, 1] = W; }
"Analysis"
The Fibonacci sequence algorithm is optimized by using the Matrix-exponentiation algorithm. The algorithm complexity is O (log2 (n)) after optimization.
Sample code (11)
The more concise code is as follows.
static double Fibonacci (int n) { Double sqrt5 = math.sqrt (5); Double phi = (1 + sqrt5)/2.0; DOUBLE fn = (Math.pow (phi, N)-Math.pow (1-phi, N))/sqrt5; return fn; }
Sample Code (a)
private static void Insertionsortinplace (int[] unsorted) {for (int i = 1; i < unsorted. Length; i++) { if (Unsorted[i-1] > Unsorted[i]) { int key = Unsorted[i]; int j = i; while (J > 0 && unsorted[j-1] > key) { unsorted[j] = unsorted[j-1]; j--; } UNSORTED[J] = key;}} }
"Analysis"
The basic operation of inserting a sort is to insert a data into the ordered data that is already sorted, thus obtaining a new ordered data. The algorithm is suitable for ordering small amounts of data, and the time complexity is O (n^2).
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Analysis of complexity of basic algorithm of data structure (II.) code example