Skip to content

refactor: update project #2226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
172 changes: 73 additions & 99 deletions basic/sorting/BubbleSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,34 @@

<!-- tabs:start -->

### **Java**
```python
def bubbleSort(arr):
n = len(arr)
# Iterate over all array elements
for i in range(n):
# Last i elements are already in place
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]


# 改进版本
def bubbleSort(arr):
n = len(arr)
for i in range(n - 1):
has_change = False
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
has_change = True
if not has_change:
break


arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print(arr)
```

```java
import java.util.Arrays;
Expand Down Expand Up @@ -42,35 +69,34 @@ public class BubbleSort {
}
```

### **JavaScript**
```cpp
#include <iostream>
#include <vector>

```js
function bubbleSort(inputArr) {
for (let i = inputArr.length - 1; i > 0; i--) {
let hasChange = false;
for (let j = 0; j < i; j++) {
if (inputArr[j] > inputArr[j + 1]) {
const temp = inputArr[j];
inputArr[j] = inputArr[j + 1];
inputArr[j + 1] = temp;
hasChange = true;
}
}
using namespace std;

if (!hasChange) {
break;
void bubbleSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
bool change = false;
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
change = true;
}
}
if (!change) break;
}

return inputArr;
}

const arr = [6, 3, 2, 1, 5];
console.log(bubbleSort(arr));
int main() {
vector<int> arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
bubbleSort(arr);
for (int v : arr) cout << v << " ";
cout << endl;
}
```

### **Go**

```go
package main

Expand All @@ -96,38 +122,6 @@ func main() {
}
```

### **C++**

```cpp
#include <iostream>
#include <vector>

using namespace std;

void bubbleSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
bool change = false;
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
change = true;
}
}
if (!change) break;
}
}

int main() {
vector<int> arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
bubbleSort(arr);
for (int v : arr) cout << v << " ";
cout << endl;
}
```

### **Rust**

```rust
fn bubble_sort(nums: &mut Vec<i32>) {
let n = nums.len();
Expand All @@ -149,7 +143,30 @@ fn main() {
}
```

### **C#**
```js
function bubbleSort(inputArr) {
for (let i = inputArr.length - 1; i > 0; i--) {
let hasChange = false;
for (let j = 0; j < i; j++) {
if (inputArr[j] > inputArr[j + 1]) {
const temp = inputArr[j];
inputArr[j] = inputArr[j + 1];
inputArr[j + 1] = temp;
hasChange = true;
}
}

if (!hasChange) {
break;
}
}

return inputArr;
}

const arr = [6, 3, 2, 1, 5];
console.log(bubbleSort(arr));
```

```cs
using static System.Console;
Expand Down Expand Up @@ -199,49 +216,6 @@ public class Program
}
```

### **Python3**

```python
def bubbleSort(arr):
n = len(arr)
# Iterate over all array elements
for i in range(n):
# Last i elements are already in place
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]


# 改进版本
def bubbleSort(arr):
n = len(arr)
for i in range(n - 1):
has_change = False
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
has_change = True
if not has_change:
break


arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print(arr)
```

<!-- tabs:end -->

## 算法分析

空间复杂度 $O(1)$、时间复杂度 $O(n^2)$。

分情况讨论:

1. 给定的数组按照顺序已经排好:只需要进行 $n-1$ 次比较,两两交换次数为 0,时间复杂度为 $O(n)$,这是最好的情况。
2. 给定的数组按照逆序排列:需要进行 $\frac{n\times (n-1)}{2}$ 次比较,时间复杂度为 $O(n^2)$,这是最坏的情况。
3. 给定的数组杂乱无章。在这种情况下,平均时间复杂度 $O(n^2)$。

因此,时间复杂度是 $O(n^2)$,这是一种稳定的排序算法。

> 稳定是指,两个相等的数,在排序过后,相对位置保持不变。
<!-- end -->
Loading