GeezerVison is showing github.com — feat: add basic sorting algorithm · JudyLee/leetcode@8bf27f7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 37 | 37 |
| 38 | 38 | 1. [冒泡排序](/basic/sorting/BubbleSort/README.md) | 39 | 39 | 1. [插入排序](/basic/sorting/InsertionSort/README.md) | | 40 | +1. [选择排序](/basic/sorting/SelectionSort/README.md) | 40 | 41 | 1. [归并排序](/basic/sorting/MergeSort/README.md) | 41 | 42 | 1. [快速排序](/basic/sorting/QuickSort/README.md) | 42 | 43 |
| |
Collapse file Copy file name to clipboardExpand all lines: README_EN.md +4-3Lines changed: 4 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | </p> | 4 | 4 |
| 5 | 5 | <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> | 8 | 8 | <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> | 9 | 9 | <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> | 10 | 10 | <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 | 35 | 35 |
| 36 | 36 | 1. [Bubble Sort](/basic/sorting/BubbleSort/README.md) | 37 | 37 | 1. [Insertion Sort](/basic/sorting/InsertionSort/README.md) | | 38 | +1. [Selection Sort](/basic/sorting/SelectionSort/README.md) | 38 | 39 | 1. [Merge Sort](/basic/sorting/MergeSort/README.md) | 39 | 40 | 1. [Quick Sort](/basic/sorting/QuickSort/README.md) | 40 | 41 |
| @@ -108,4 +109,4 @@ Thank you to all our backers and sponsors! | 108 | 109 |
| 109 | 110 | 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>. | 110 | 111 |
| 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 | +[](https://app.fossa.com/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode?ref=badge_large) |
Collapse file Copy file name to clipboardExpand all lines: basic/README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 |
| 5 | 5 | - [冒泡排序](./sorting/BubbleSort/README.md) | 6 | 6 | - [插入排序](./sorting/InsertionSort/README.md) | | 7 | +- [选择排序](./sorting/SelectionSort/README.md) | 7 | 8 | - [归并排序](./sorting/MergeSort/README.md) | 8 | 9 | - [快速排序](./sorting/QuickSort/README.md) |
Collapse file Copy file name to clipboardExpand all lines: basic/README_EN.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 |
| 5 | 5 | - [Bubble Sort](./sorting/BubbleSort/README.md) | 6 | 6 | - [Insertion Sort](./sorting/InsertionSort/README.md) | | 7 | +- [Selection Sort](./sorting/SelectionSort/README.md) | 7 | 8 | - [Merge Sort](./sorting/MergeSort/README.md) | 8 | 9 | - [Quick Sort](./sorting/QuickSort/README.md) |
Collapse file Copy file name to clipboardExpand all lines: basic/sorting/BubbleSort/README.md -2Lines changed: 0 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 |
| 9 | 9 | <!-- tabs:start --> | 10 | 10 |
| 11 | | -### **Python3** | 12 | | - | 13 | 11 | ### **Java** | 14 | 12 |
| 15 | 13 | ```java | |
Collapse file Copy file name to clipboardExpand all lines: basic/sorting/InsertionSort/README.md +10-6Lines changed: 10 additions & 6 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | # 插入排序 | 2 | 2 |
| | 3 | +先来看一个问题。一个有序的数组,我们往里面添加一个新的数据后,如何继续保持数据有序呢?很简单,我们只要遍历数组,找到数据应该插入的位置将其插入即可。 | | 4 | + | | 5 | +这是一个动态排序的过程,即动态地往有序集合中添加数据,我们可以通过这种方法保持集合中的数据一直有序。而对于一组静态数据,我们也可以借鉴上面讲的插入方法,来进行排序,于是就有了插入排序算法。 | | 6 | + | | 7 | +那么插入排序具体是如何借助上面的思想来实现排序的呢? | | 8 | + | | 9 | +首先,我们将数组中的数据分为两个区间,**已排序区间**和**未排序区间**。初始已排序区间只有一个元素,就是数组的第一个元素。插入算法的核心思想是取未排序区间中的元素,在已排序区间中找到合适的插入位置将其插入,并保证已排序区间数据一直有序。重复这个过程,直到未排序区间中元素为空,算法结束。 | | 10 | + | 3 | 11 | 与冒泡排序对比: | 4 | 12 |
| 5 | 13 | - 在冒泡排序中,经过每一轮的排序处理后,数组后端的数是排好序的。 | 6 | 14 | - 在插入排序中,经过每一轮的排序处理后,数组前端的数是排好序的。 | 7 | 15 |
| 8 | | -插入排序的算法思想是:不断将尚未排好序的数插入到已经排好序的部分。 | 9 | | - | 10 | 16 | ## 代码示例 | 11 | 17 |
| 12 | 18 | <!-- tabs:start --> | 13 | 19 |
| 14 | | -### **Python3** | 15 | | - | 16 | 20 | ### **Java** | 17 | 21 |
| 18 | 22 | ```java | @@ -47,7 +51,7 @@ public class InsertionSort { | 47 | 51 | 分情况讨论: | 48 | 52 |
| 49 | 53 | 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²)。 | 52 | 56 |
| 53 | 57 | 因此,时间复杂度是 O(n²),这也是一种稳定的排序算法。 |
Collapse file Copy file name to clipboardExpand all lines: basic/sorting/MergeSort/README.md -2Lines changed: 0 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 |
| 9 | 9 | <!-- tabs:start --> | 10 | 10 |
| 11 | | -### **Python3** | 12 | | - | 13 | 11 | ### **Java** | 14 | 12 |
| 15 | 13 | ```java | |
Collapse file Copy file name to clipboardExpand all lines: basic/sorting/QuickSort/README.md -2Lines changed: 0 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 |
| 7 | 7 | <!-- tabs:start --> | 8 | 8 |
| 9 | | -### **Python3** | 10 | | - | 11 | 9 | ### **Java** | 12 | 10 |
| 13 | 11 | ```java | |
Collapse file Copy file name to clipboardExpand all lines: basic/sorting/README.md Collapse file Copy file name to clipboard +53Lines changed: 53 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 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 顺序就变了,所以就不稳定了。正是因此,相对于冒泡排序和插入排序,选择排序就稍微逊色了。 |
|