Skip to content

Commit 8bf27f7

Browse files
committed
feat: add basic sorting algorithm
1 parent 7791ff7 commit 8bf27f7

File tree

12 files changed

+99
-15
lines changed

12 files changed

+99
-15
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
1. [冒泡排序](/basic/sorting/BubbleSort/README.md)
3939
1. [插入排序](/basic/sorting/InsertionSort/README.md)
40+
1. [选择排序](/basic/sorting/SelectionSort/README.md)
4041
1. [归并排序](/basic/sorting/MergeSort/README.md)
4142
1. [快速排序](/basic/sorting/QuickSort/README.md)
4243

README_EN.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
</p>
44

55
<p align="center">
6-
<a href="https://github.com/doocs/leetcode"><img src="https://badgen.net/badge/langs/Java,C++,Python,JavaScript,Go,.../green?list=1" alt="languages"></a>
7-
<a href="https://opencollective.com/doocs-leetcode#backers" alt="backers on Open Collective"><img src="https://opencollective.com/doocs-leetcode/backers/badge.svg" /></a>
6+
<a href="https://github.com/doocs/leetcode"><img src="https://badgen.net/badge/langs/Java,Python,C++,JavaScript,Go,.../green?list=1" alt="languages"></a>
7+
<a href="https://opencollective.com/doocs-leetcode/backers/badge.svg" alt="backers on Open Collective"><img src="https://opencollective.com/doocs-leetcode/backers/badge.svg" /></a>
88
<a href="https://opencollective.com/doocs-leetcode/sponsors/badge.svg" alt="Sponsors on Open Collective"><img src="https://opencollective.com/doocs-leetcode/sponsors/badge.svg" /></a>
99
<a href="https://github.com/doocs/leetcode/blob/main/LICENSE"><img src="https://badgen.net/github/license/doocs/leetcode?color=green" alt="LICENSE"></a><br>
1010
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode" alt="stars"></a>
@@ -35,6 +35,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
3535

3636
1. [Bubble Sort](/basic/sorting/BubbleSort/README.md)
3737
1. [Insertion Sort](/basic/sorting/InsertionSort/README.md)
38+
1. [Selection Sort](/basic/sorting/SelectionSort/README.md)
3839
1. [Merge Sort](/basic/sorting/MergeSort/README.md)
3940
1. [Quick Sort](/basic/sorting/QuickSort/README.md)
4041

@@ -108,4 +109,4 @@ Thank you to all our backers and sponsors!
108109

109110
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>.
110111

111-
<a rel="license" href="https://github.com/doocs/leetcode/blob/main/LICENSE"><img alt="Creative Commons License" style="border-width:0" src="./images/cc-by-sa-88x31.png" /></a>
112+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode?ref=badge_large)

basic/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
- [冒泡排序](./sorting/BubbleSort/README.md)
66
- [插入排序](./sorting/InsertionSort/README.md)
7+
- [选择排序](./sorting/SelectionSort/README.md)
78
- [归并排序](./sorting/MergeSort/README.md)
89
- [快速排序](./sorting/QuickSort/README.md)

basic/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
- [Bubble Sort](./sorting/BubbleSort/README.md)
66
- [Insertion Sort](./sorting/InsertionSort/README.md)
7+
- [Selection Sort](./sorting/SelectionSort/README.md)
78
- [Merge Sort](./sorting/MergeSort/README.md)
89
- [Quick Sort](./sorting/QuickSort/README.md)

basic/sorting/BubbleSort/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
<!-- tabs:start -->
1010

11-
### **Python3**
12-
1311
### **Java**
1412

1513
```java

basic/sorting/InsertionSort/README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
# 插入排序
22

3+
先来看一个问题。一个有序的数组,我们往里面添加一个新的数据后,如何继续保持数据有序呢?很简单,我们只要遍历数组,找到数据应该插入的位置将其插入即可。
4+
5+
这是一个动态排序的过程,即动态地往有序集合中添加数据,我们可以通过这种方法保持集合中的数据一直有序。而对于一组静态数据,我们也可以借鉴上面讲的插入方法,来进行排序,于是就有了插入排序算法。
6+
7+
那么插入排序具体是如何借助上面的思想来实现排序的呢?
8+
9+
首先,我们将数组中的数据分为两个区间,**已排序区间****未排序区间**。初始已排序区间只有一个元素,就是数组的第一个元素。插入算法的核心思想是取未排序区间中的元素,在已排序区间中找到合适的插入位置将其插入,并保证已排序区间数据一直有序。重复这个过程,直到未排序区间中元素为空,算法结束。
10+
311
与冒泡排序对比:
412

513
- 在冒泡排序中,经过每一轮的排序处理后,数组后端的数是排好序的。
614
- 在插入排序中,经过每一轮的排序处理后,数组前端的数是排好序的。
715

8-
插入排序的算法思想是:不断将尚未排好序的数插入到已经排好序的部分。
9-
1016
## 代码示例
1117

1218
<!-- tabs:start -->
1319

14-
### **Python3**
15-
1620
### **Java**
1721

1822
```java
@@ -47,7 +51,7 @@ public class InsertionSort {
4751
分情况讨论:
4852

4953
1. 给定的数组按照顺序排好序:只需要进行 n-1 次比较,两两交换次数为 0,时间复杂度为 O(n),这是最好的情况。
50-
2. 给定的数组按照逆序排列:需要进行 `n*(n-1)/2` 次比较,时间复杂度为 O(n²),这是最坏的情况。
51-
3. 给定的数组杂乱无章:在这种情况下,平均时间复杂度是 O(n²)。
54+
1. 给定的数组按照逆序排列:需要进行 `n*(n-1)/2` 次比较,时间复杂度为 O(n²),这是最坏的情况。
55+
1. 给定的数组杂乱无章:在这种情况下,平均时间复杂度是 O(n²)。
5256

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

basic/sorting/MergeSort/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
<!-- tabs:start -->
1010

11-
### **Python3**
12-
1311
### **Java**
1412

1513
```java

basic/sorting/QuickSort/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
<!-- tabs:start -->
88

9-
### **Python3**
10-
119
### **Java**
1210

1311
```java

basic/sorting/README.md

Whitespace-only changes.

basic/sorting/SelectionSort/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 选择排序
2+
3+
选择排序算法的实现思路有点类似插入排序,也分已排序区间和未排序区间。但是选择排序每次会从未排序区间中找到最小的元素,将其放到已排序区间的末尾。
4+
5+
## 代码示例
6+
7+
<!-- tabs:start -->
8+
9+
### **Java**
10+
11+
```java
12+
import java.util.Arrays;
13+
14+
public class SelectionSort {
15+
16+
private static void selectionSort(int[] nums) {
17+
for (int i = 0, n = nums.length; i < n - 1; ++i) {
18+
int minIndex = i;
19+
for (int j = i; j < n; ++j) {
20+
if (nums[j] < nums[minIndex]) {
21+
minIndex = j;
22+
}
23+
}
24+
swap(nums, minIndex, i);
25+
}
26+
}
27+
28+
private static void swap(int[] nums, int i, int j) {
29+
int t = nums[i];
30+
nums[i] = nums[j];
31+
nums[j] = t;
32+
}
33+
34+
public static void main(String[] args) {
35+
int[] nums = {1, 2, 7, 9, 5, 8};
36+
selectionSort(nums);
37+
System.out.println(Arrays.toString(nums));
38+
}
39+
}
40+
41+
```
42+
43+
<!-- tabs:end -->
44+
45+
## 算法分析
46+
47+
空间复杂度 O(1),时间复杂度 O(n²)。
48+
49+
那选择排序是稳定的排序算法吗?
50+
51+
答案是否定的,**选择排序是一种不稳定的排序算法**。选择排序每次都要找剩余未排序元素中的最小值,并和前面的元素交换位置,这样破坏了稳定性。
52+
53+
比如 5,8,5,2,9 这样一组数据,使用选择排序算法来排序的话,第一次找到最小元素 2,与第一个 5 交换位置,那第一个 5 和中间的 5 顺序就变了,所以就不稳定了。正是因此,相对于冒泡排序和插入排序,选择排序就稍微逊色了。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Arrays;
2+
3+
public class SelectionSort {
4+
5+
private static void selectionSort(int[] nums) {
6+
for (int i = 0, n = nums.length; i < n - 1; ++i) {
7+
int minIndex = i;
8+
for (int j = i; j < n; ++j) {
9+
if (nums[j] < nums[minIndex]) {
10+
minIndex = j;
11+
}
12+
}
13+
swap(nums, minIndex, i);
14+
}
15+
}
16+
17+
private static void swap(int[] nums, int i, int j) {
18+
int t = nums[i];
19+
nums[i] = nums[j];
20+
nums[j] = t;
21+
}
22+
23+
public static void main(String[] args) {
24+
int[] nums = {1, 2, 7, 9, 5, 8};
25+
selectionSort(nums);
26+
System.out.println(Arrays.toString(nums));
27+
}
28+
}

basic/summary.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
- 常见的排序算法
33
- [冒泡排序](/basic/sorting/BubbleSort/README.md)
44
- [插入排序](/basic/sorting/InsertionSort/README.md)
5+
- [选择排序](/basic/sorting/SelectionSort/README.md)
56
- [归并排序](/basic/sorting/MergeSort/README.md)
67
- [快速排序](/basic/sorting/QuickSort/README.md)

0 commit comments

Comments
 (0)