mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Fix Repo
This commit is contained in:
@@ -801,14 +801,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 常数阶 */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
[class]{}-[func]{constant}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -927,13 +920,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 线性阶 */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
[class]{}-[func]{linear}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -1045,15 +1032,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 线性阶(遍历数组) */
|
||||
int arrayTraversal(vector<int>& nums) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成正比
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
[class]{}-[func]{arrayTraversal}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -1175,17 +1154,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 平方阶 */
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
[class]{}-[func]{quadratic}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -1330,24 +1299,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 平方阶(冒泡排序) */
|
||||
int bubbleSort(vector<int>& nums) {
|
||||
int count = 0; // 计数器
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = nums.size() - 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;
|
||||
}
|
||||
[class]{}-[func]{bubbleSort}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -1543,19 +1495,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 指数阶(循环实现) */
|
||||
int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < base; j++) {
|
||||
count++;
|
||||
}
|
||||
base *= 2;
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count;
|
||||
}
|
||||
[class]{}-[func]{exponential}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -1716,11 +1656,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
[class]{}-[func]{expRecur}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -1822,15 +1758,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 对数阶(循环实现) */
|
||||
int logarithmic(float n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
[class]{}-[func]{logarithmic}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -1958,11 +1886,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
[class]{}-[func]{logRecur}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -2062,16 +1986,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 线性对数阶 */
|
||||
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;
|
||||
}
|
||||
[class]{}-[func]{linearLogRecur}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@@ -2213,16 +2128,7 @@ $$
|
||||
=== "C++"
|
||||
|
||||
```cpp title="time_complexity.cpp"
|
||||
/* 阶乘阶(递归实现) */
|
||||
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;
|
||||
}
|
||||
[class]{}-[func]{factorialRecur}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
Reference in New Issue
Block a user