标签:算法复杂度 时间复杂度 空间复杂度 渐进复杂度 渐近符
示例代码(1)decimal Factorial(int n) { if (n == 0) return 1; else return n * Factorial(n - 1); }【分析】
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; }【分析】
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; }【分析】
long SumMN(int n, int m) { long sum = 0; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) sum += x * y; return sum; }【分析】
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; }【分析】
decimal Calculation(int n) { decimal result = 0; for (int i = 0; i < (1 << n); i++) result += i; return result; }【分析】
int Fibonacci(int n) { if (n <= 1) return n; else return Fibonacci(n - 1) + Fibonacci(n - 2); }【分析】
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]; } }【分析】
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; } }【分析】
示例代码(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; }【分析】
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; }
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; } } }【分析】
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:算法复杂度 时间复杂度 空间复杂度 渐进复杂度 渐近符
原文地址:http://blog.csdn.net/u013630349/article/details/47210431