mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
feat(csharp) .NET 8.0 code migration (#966)
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -9,7 +9,7 @@ namespace hello_algo.chapter_computational_complexity;
|
||||
public class time_complexity {
|
||||
void Algorithm(int n) {
|
||||
int a = 1; // +0(技巧 1)
|
||||
a = a + n; // +0(技巧 1)
|
||||
a += n; // +0(技巧 1)
|
||||
// +n(技巧 2)
|
||||
for (int i = 0; i < 5 * n + 1; i++) {
|
||||
Console.WriteLine(0);
|
||||
@@ -26,12 +26,14 @@ public class time_complexity {
|
||||
void AlgorithmA(int n) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
|
||||
// 算法 B 时间复杂度:线性阶
|
||||
void AlgorithmB(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
|
||||
// 算法 C 时间复杂度:常数阶
|
||||
void AlgorithmC(int n) {
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
@@ -40,7 +42,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 常数阶 */
|
||||
static int Constant(int n) {
|
||||
int Constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
@@ -49,7 +51,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 线性阶 */
|
||||
static int Linear(int n) {
|
||||
int Linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
@@ -57,7 +59,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
static int ArrayTraversal(int[] nums) {
|
||||
int ArrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
foreach (int num in nums) {
|
||||
@@ -67,7 +69,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
static int Quadratic(int n) {
|
||||
int Quadratic(int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -79,7 +81,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 平方阶(冒泡排序) */
|
||||
static int BubbleSort(int[] nums) {
|
||||
int BubbleSort(int[] nums) {
|
||||
int count = 0; // 计数器
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
for (int i = nums.Length - 1; i > 0; i--) {
|
||||
@@ -96,7 +98,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 指数阶(循环实现) */
|
||||
static 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++) {
|
||||
@@ -110,29 +112,29 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
static int ExpRecur(int n) {
|
||||
int ExpRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
static int Logarithmic(float n) {
|
||||
int Logarithmic(float n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
n /= 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
static int LogRecur(float n) {
|
||||
int LogRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
return LogRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
static int LinearLogRecur(float n) {
|
||||
int LinearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -142,7 +144,7 @@ public class time_complexity {
|
||||
}
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
static int FactorialRecur(int n) {
|
||||
int FactorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
|
||||
Reference in New Issue
Block a user