mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-15 08:26:06 +00:00
build
This commit is contained in:
@@ -59,7 +59,7 @@ comments: true
|
||||
|
||||
```csharp title=""
|
||||
// 在某运行平台下
|
||||
void algorithm(int n) {
|
||||
void Algorithm(int n) {
|
||||
int a = 2; // 1 ns
|
||||
a = a + 1; // 1 ns
|
||||
a = a * 2; // 10 ns
|
||||
@@ -257,17 +257,17 @@ $$
|
||||
|
||||
```csharp title=""
|
||||
// 算法 A 的时间复杂度:常数阶
|
||||
void algorithm_A(int n) {
|
||||
void AlgorithmA(int n) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
// 算法 B 的时间复杂度:线性阶
|
||||
void algorithm_B(int n) {
|
||||
void AlgorithmB(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
// 算法 C 的时间复杂度:常数阶
|
||||
void algorithm_C(int n) {
|
||||
void AlgorithmC(int n) {
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
@@ -493,7 +493,7 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
void algorithm(int n) {
|
||||
void Algorithm(int n) {
|
||||
int a = 1; // +1
|
||||
a = a + 1; // +1
|
||||
a = a * 2; // +1
|
||||
@@ -703,7 +703,7 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
void algorithm(int n) {
|
||||
void Algorithm(int n) {
|
||||
int a = 1; // +0(技巧 1)
|
||||
a = a + n; // +0(技巧 1)
|
||||
// +n(技巧 2)
|
||||
@@ -953,7 +953,7 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 常数阶 */
|
||||
int constant(int n) {
|
||||
int Constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
@@ -1117,7 +1117,7 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 线性阶 */
|
||||
int linear(int n) {
|
||||
int Linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
@@ -1272,7 +1272,7 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(int[] nums) {
|
||||
int ArrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
foreach (int num in nums) {
|
||||
@@ -1449,7 +1449,7 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 平方阶 */
|
||||
int quadratic(int n) {
|
||||
int Quadratic(int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -1668,7 +1668,7 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(int[] nums) {
|
||||
int BubbleSort(int[] nums) {
|
||||
int count = 0; // 计数器
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
@@ -1933,7 +1933,7 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 指数阶(循环实现) */
|
||||
int exponential(int n) {
|
||||
int Exponential(int n) {
|
||||
int count = 0, bas = 1;
|
||||
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -2141,9 +2141,9 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
int ExpRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -2286,7 +2286,7 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 对数阶(循环实现) */
|
||||
int logarithmic(float n) {
|
||||
int Logarithmic(float n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
@@ -2453,9 +2453,9 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
int LogRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
return LogRecur(n / 2) + 1;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -2610,10 +2610,10 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n) {
|
||||
int LinearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
int count = LinearLogRecur(n / 2) +
|
||||
LinearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
@@ -2812,12 +2812,12 @@ $$
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 阶乘阶(递归实现) */
|
||||
int factorialRecur(int n) {
|
||||
int FactorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
count += FactorialRecur(n - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@@ -3051,7 +3051,7 @@ $$
|
||||
|
||||
```csharp title="worst_best_time_complexity.cs"
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
int[] randomNumbers(int n) {
|
||||
int[] RandomNumbers(int n) {
|
||||
int[] nums = new int[n];
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -3070,7 +3070,7 @@ $$
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
int findOne(int[] nums) {
|
||||
int FindOne(int[] nums) {
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
|
||||
Reference in New Issue
Block a user