mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Add sorce code blocks of C to the docs.
This commit is contained in:
@@ -86,23 +86,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="bubble_sort.c"
|
||||
/* 冒泡排序 */
|
||||
void bubbleSort(int nums[], int size) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = 0; i < size - 1; i++)
|
||||
{
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < size - 1 - i; j++)
|
||||
{
|
||||
if (nums[j] > nums[j + 1])
|
||||
{
|
||||
int temp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
[class]{}-[func]{bubbleSort}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -180,26 +164,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="bubble_sort.c"
|
||||
/* 冒泡排序 */
|
||||
void bubbleSortWithFlag(int nums[], int size) {
|
||||
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
|
||||
for (int i = 0; i < size - 1; i++)
|
||||
{
|
||||
bool flag = false;
|
||||
// 内循环:冒泡操作
|
||||
for (int j = 0; j < size - 1 - i; j++)
|
||||
{
|
||||
if (nums[j] > nums[j + 1])
|
||||
{
|
||||
int temp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = temp;
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
if(!flag) break;
|
||||
}
|
||||
}
|
||||
[class]{}-[func]{bubbleSortWithFlag}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@@ -63,23 +63,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="insertion_sort.c"
|
||||
/* 插入排序 */
|
||||
void insertionSort(int nums[], int size) {
|
||||
// 外循环:base = nums[1], nums[2], ..., nums[n-1]
|
||||
for (int i = 1; i < size; i++)
|
||||
{
|
||||
int base = nums[i], j = i - 1;
|
||||
// 内循环:将 base 插入到左边的正确位置
|
||||
while (j >= 0 && nums[j] > base)
|
||||
{
|
||||
// 1. 将 nums[j] 向右移动一位
|
||||
nums[j + 1] = nums[j];
|
||||
j--;
|
||||
}
|
||||
// 2. 将 base 赋值到正确位置
|
||||
nums[j + 1] = base;
|
||||
}
|
||||
}
|
||||
[class]{}-[func]{insertionSort}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@@ -90,7 +90,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="quick_sort.c"
|
||||
|
||||
[class]{quickSort}-[func]{partition}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -172,7 +172,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="quick_sort.c"
|
||||
|
||||
[class]{quickSort}-[func]{quickSort}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -274,7 +274,9 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="quick_sort.c"
|
||||
[class]{quickSortMedian}-[func]{medianThree}
|
||||
|
||||
[class]{quickSortMedian}-[func]{partition}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -346,7 +348,7 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="quick_sort.c"
|
||||
|
||||
[class]{quickSortTailCall}-[func]{quickSort}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
Reference in New Issue
Block a user