Sort the coding languages by applications. (#721)

This commit is contained in:
Yudong Jin
2023-09-04 03:19:08 +08:00
committed by GitHub
parent 8d5e84f70a
commit 9c3b7b6422
55 changed files with 6826 additions and 6826 deletions
+60 -60
View File
@@ -36,10 +36,10 @@
![冒泡排序流程](bubble_sort.assets/bubble_sort_overview.png)
=== "Java"
=== "Python"
```java title="bubble_sort.java"
[class]{bubble_sort}-[func]{bubbleSort}
```python title="bubble_sort.py"
[class]{}-[func]{bubble_sort}
```
=== "C++"
@@ -48,10 +48,16 @@
[class]{}-[func]{bubbleSort}
```
=== "Python"
=== "Java"
```python title="bubble_sort.py"
[class]{}-[func]{bubble_sort}
```java title="bubble_sort.java"
[class]{bubble_sort}-[func]{bubbleSort}
```
=== "C#"
```csharp title="bubble_sort.cs"
[class]{bubble_sort}-[func]{bubbleSort}
```
=== "Go"
@@ -60,6 +66,12 @@
[class]{}-[func]{bubbleSort}
```
=== "Swift"
```swift title="bubble_sort.swift"
[class]{}-[func]{bubbleSort}
```
=== "JS"
```javascript title="bubble_sort.js"
@@ -72,30 +84,6 @@
[class]{}-[func]{bubbleSort}
```
=== "C"
```c title="bubble_sort.c"
[class]{}-[func]{bubbleSort}
```
=== "C#"
```csharp title="bubble_sort.cs"
[class]{bubble_sort}-[func]{bubbleSort}
```
=== "Swift"
```swift title="bubble_sort.swift"
[class]{}-[func]{bubbleSort}
```
=== "Zig"
```zig title="bubble_sort.zig"
[class]{}-[func]{bubbleSort}
```
=== "Dart"
```dart title="bubble_sort.dart"
@@ -108,16 +96,28 @@
[class]{}-[func]{bubble_sort}
```
=== "C"
```c title="bubble_sort.c"
[class]{}-[func]{bubbleSort}
```
=== "Zig"
```zig title="bubble_sort.zig"
[class]{}-[func]{bubbleSort}
```
## 效率优化
我们发现,如果某轮“冒泡”中没有执行任何交换操作,说明数组已经完成排序,可直接返回结果。因此,可以增加一个标志位 `flag` 来监测这种情况,一旦出现就立即返回。
经过优化,冒泡排序的最差和平均时间复杂度仍为 $O(n^2)$ ;但当输入数组完全有序时,可达到最佳时间复杂度 $O(n)$ 。
=== "Java"
=== "Python"
```java title="bubble_sort.java"
[class]{bubble_sort}-[func]{bubbleSortWithFlag}
```python title="bubble_sort.py"
[class]{}-[func]{bubble_sort_with_flag}
```
=== "C++"
@@ -126,10 +126,16 @@
[class]{}-[func]{bubbleSortWithFlag}
```
=== "Python"
=== "Java"
```python title="bubble_sort.py"
[class]{}-[func]{bubble_sort_with_flag}
```java title="bubble_sort.java"
[class]{bubble_sort}-[func]{bubbleSortWithFlag}
```
=== "C#"
```csharp title="bubble_sort.cs"
[class]{bubble_sort}-[func]{bubbleSortWithFlag}
```
=== "Go"
@@ -138,6 +144,12 @@
[class]{}-[func]{bubbleSortWithFlag}
```
=== "Swift"
```swift title="bubble_sort.swift"
[class]{}-[func]{bubbleSortWithFlag}
```
=== "JS"
```javascript title="bubble_sort.js"
@@ -150,30 +162,6 @@
[class]{}-[func]{bubbleSortWithFlag}
```
=== "C"
```c title="bubble_sort.c"
[class]{}-[func]{bubbleSortWithFlag}
```
=== "C#"
```csharp title="bubble_sort.cs"
[class]{bubble_sort}-[func]{bubbleSortWithFlag}
```
=== "Swift"
```swift title="bubble_sort.swift"
[class]{}-[func]{bubbleSortWithFlag}
```
=== "Zig"
```zig title="bubble_sort.zig"
[class]{}-[func]{bubbleSortWithFlag}
```
=== "Dart"
```dart title="bubble_sort.dart"
@@ -186,6 +174,18 @@
[class]{}-[func]{bubble_sort_with_flag}
```
=== "C"
```c title="bubble_sort.c"
[class]{}-[func]{bubbleSortWithFlag}
```
=== "Zig"
```zig title="bubble_sort.zig"
[class]{}-[func]{bubbleSortWithFlag}
```
## 算法特性
- **时间复杂度为 $O(n^2)$、自适应排序**:各轮“冒泡”遍历的数组长度依次为 $n - 1$、$n - 2$、$\dots$、$2$、$1$ ,总和为 $(n - 1) n / 2$ 。在引入 `flag` 优化后,最佳时间复杂度可达到 $O(n)$ 。