mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-10 14:36:06 +00:00
完善所以c#相关的文档和代码
This commit is contained in:
@@ -97,7 +97,18 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
|
||||
// 在某运行平台下
|
||||
void algorithm(int n)
|
||||
{
|
||||
int a = 2; // 1 ns
|
||||
a = a + 1; // 1 ns
|
||||
a = a * 2; // 10 ns
|
||||
// 循环 n 次
|
||||
for (int i = 0; i < n; i++)
|
||||
{ // 1 ns ,每轮都要执行 i++
|
||||
Console.WriteLine(0); // 5 ns
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
但实际上, **统计算法的运行时间既不合理也不现实。** 首先,我们不希望预估时间和运行平台绑定,毕竟算法需要跑在各式各样的平台之上。其次,我们很难获知每一种操作的运行时间,这为预估过程带来了极大的难度。
|
||||
@@ -212,7 +223,27 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
|
||||
// 算法 A 时间复杂度:常数阶
|
||||
void algorithm_A(int n)
|
||||
{
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
// 算法 B 时间复杂度:线性阶
|
||||
void algorithm_B(int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
// 算法 C 时间复杂度:常数阶
|
||||
void algorithm_C(int n)
|
||||
{
|
||||
for (int i = 0; i < 1000000; i++)
|
||||
{
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -310,7 +341,15 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
|
||||
void algorithm(int n) {
|
||||
int a = 1; // +1
|
||||
a = a + 1; // +1
|
||||
a = a * 2; // +1
|
||||
// 循环 n 次
|
||||
for (int i = 0; i < n; i++) { // +1(每轮都执行 i ++)
|
||||
Console.WriteLine(0); // +1
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
$T(n)$ 是个一次函数,说明时间增长趋势是线性的,因此易得时间复杂度是线性阶。
|
||||
@@ -457,7 +496,24 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title=""
|
||||
|
||||
void algorithm(int n)
|
||||
{
|
||||
int a = 1; // +0(技巧 1)
|
||||
a = a + n; // +0(技巧 1)
|
||||
// +n(技巧 2)
|
||||
for (int i = 0; i < 5 * n + 1; i++)
|
||||
{
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
// +n*n(技巧 3)
|
||||
for (int i = 0; i < 2 * n; i++)
|
||||
{
|
||||
for (int j = 0; j < n + 1; j++)
|
||||
{
|
||||
Console.WriteLine(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 判断渐近上界
|
||||
@@ -576,7 +632,15 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 常数阶 */
|
||||
int constant(int n)
|
||||
{
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
### 线性阶 $O(n)$
|
||||
@@ -652,7 +716,14 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 线性阶 */
|
||||
int linear(int n)
|
||||
{
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
「遍历数组」和「遍历链表」等操作,时间复杂度都为 $O(n)$ ,其中 $n$ 为数组或链表的长度。
|
||||
@@ -736,7 +807,17 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(int[] nums)
|
||||
{
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
foreach(int num in nums)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||
### 平方阶 $O(n^2)$
|
||||
@@ -825,7 +906,20 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 平方阶 */
|
||||
int quadratic(int n)
|
||||
{
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -947,6 +1041,28 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(int[] nums)
|
||||
{
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.Length - 1; i > 0; i--)
|
||||
{
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
if (nums[j] > nums[j + 1])
|
||||
{
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -1048,7 +1164,22 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 指数阶(循环实现) */
|
||||
int exponential(int n)
|
||||
{
|
||||
int count = 0, bas = 1;
|
||||
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < bas; j++)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
bas *= 2;
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -1119,7 +1250,12 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n)
|
||||
{
|
||||
if (n == 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
```
|
||||
|
||||
### 对数阶 $O(\log n)$
|
||||
@@ -1205,7 +1341,17 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
int logarithmic(float n)
|
||||
{
|
||||
int count = 0;
|
||||
while (n > 1)
|
||||
{
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -1276,7 +1422,12 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n)
|
||||
{
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
```
|
||||
|
||||
### 线性对数阶 $O(n \log n)$
|
||||
@@ -1366,7 +1517,18 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n)
|
||||
{
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -1464,7 +1626,18 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="time_complexity.cs"
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
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);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
```
|
||||
|
||||

|
||||
@@ -1485,7 +1658,7 @@ $$
|
||||
```java title="worst_best_time_complexity.java"
|
||||
public class worst_best_time_complexity {
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
static int[] randomNumbers(int n) {
|
||||
int[] randomNumbers(int n) {
|
||||
Integer[] nums = new Integer[n];
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -1502,7 +1675,7 @@ $$
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
static int findOne(int[] nums) {
|
||||
int findOne(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
@@ -1511,7 +1684,7 @@ $$
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
public void main(String[] args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
@@ -1652,7 +1825,51 @@ $$
|
||||
=== "C#"
|
||||
|
||||
```csharp title="worst_best_time_complexity.cs"
|
||||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
|
||||
int[] randomNumbers(int n)
|
||||
{
|
||||
int[] nums = new int[n];
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
|
||||
// 随机打乱数组元素
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
var index = new Random().Next(i, nums.Length);
|
||||
var tmp = nums[i];
|
||||
var ran = nums[index];
|
||||
nums[i] = ran;
|
||||
nums[index] = tmp;
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
int findOne(int[] nums)
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public void main(String[] args)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
Console.WriteLine("\n数组 [ 1, 2, ..., n ] 被打乱后 = " + string.Join(",", nums));
|
||||
Console.WriteLine("数字 1 的索引为 " + index);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
!!! tip
|
||||
|
||||
Reference in New Issue
Block a user