-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/epoch-lab/cuit-guide
- Loading branch information
Showing
8 changed files
with
873 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,4 +137,7 @@ dist | |
.DS_Store | ||
|
||
# Local history | ||
.history | ||
.history | ||
|
||
# IntelliJ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
**本模块是公共类课程,适合绝大部分计算机岗位。由于 Java 受众最广且易读性强,本模块示例代码使用 Java 编写,其他语言也可参考学习。** | ||
|
||
## 冒泡排序原理 | ||
|
||
冒泡排序是一种简单的排序算法,主要思想是从头到尾依次比较相邻的两个元素,如果逆序则交换,直到没有逆序的元素为止。 | ||
|
||
冒泡排序的时间复杂度是 O(n^2),空间复杂度是 O(1)。 | ||
|
||
## 冒泡排序实现 | ||
|
||
如,对数组`arr = [5, 3, 8, 6, 4]`进行升序冒泡排序。 | ||
|
||
设置`end`指向数组末尾,每一轮比较结束后,`end--`,或者说,`end = arr.length - 轮数 - 1`。 | ||
|
||
每一轮让指针`cur`指向数组第 0 个元素,然后依次比较。 | ||
|
||
最后设置一个标志位`flag`,每一轮都初始化为`false`,如果某一轮没有发生交换,则说明数组已经有序,直接跳出循环。 | ||
|
||
排序过程如下: | ||
|
||
1. 第一轮 | ||
|
||
| 5 | 3 | 8 | 6 | 4 | | ||
| --- | ---- | --- | --- | --- | | ||
| cur | next | | | | | ||
|
||
`arr[cur] > arr[next]`,交换位置,`flag = true` | ||
|
||
| 3 | 5 | 8 | 6 | 4 | | ||
| --- | --- | ---- | --- | --- | | ||
| | cur | next | | | | ||
|
||
`arr[cur] < arr[next]`,不交换位置 | ||
|
||
| 3 | 5 | 8 | 6 | 4 | | ||
| --- | --- | --- | ---- | --- | | ||
| | | cur | next | | | ||
|
||
`arr[cur] > arr[next]`,交换位置 | ||
|
||
| 3 | 5 | 6 | 8 | 4 | | ||
| --- | --- | --- | --- | ---- | | ||
| | | | cur | next | | ||
|
||
`arr[cur] > arr[next]`,交换位置 | ||
|
||
| 3 | 5 | 6 | 4 | 8 | | ||
| --- | --- | --- | --- | --- | | ||
| | | | | cur | | ||
|
||
`cur`位于`end`位置,跳出循环,`end--` | ||
|
||
2. 第二轮 | ||
|
||
| 3 | 5 | 6 | 4 | 8 | | ||
| --- | ---- | --- | --- | --- | | ||
| cur | next | | | | | ||
|
||
`arr[cur] < arr[next]`,不交换位置 | ||
|
||
| 3 | 5 | 6 | 4 | 8 | | ||
| --- | --- | ---- | --- | --- | | ||
| | cur | next | | | | ||
|
||
`arr[cur] < arr[next]`,不交换位置 | ||
|
||
| 3 | 5 | 6 | 4 | 8 | | ||
| --- | --- | --- | ---- | --- | | ||
| | | cur | next | | | ||
|
||
`arr[cur] > arr[next]`,交换位置,`flag = true` | ||
|
||
| 3 | 5 | 4 | 6 | 8 | | ||
| --- | --- | --- | --- | --- | | ||
| | | | cur | | | ||
|
||
`cur`位于`end`位置,跳出循环,`end--` | ||
|
||
3. 第三轮 | ||
|
||
| 3 | 5 | 4 | 6 | 8 | | ||
| --- | ---- | --- | --- | --- | | ||
| cur | next | | | | | ||
|
||
`arr[cur] < arr[next]`,不交换位置 | ||
|
||
| 3 | 5 | 4 | 6 | 8 | | ||
| --- | --- | ---- | --- | --- | | ||
| | cur | next | | | | ||
|
||
`arr[cur] > arr[next]`,交换位置,`flag = true` | ||
|
||
| 3 | 4 | 5 | 6 | 8 | | ||
| --- | --- | --- | --- | --- | | ||
| | | cur | | | | ||
|
||
`cur`位于`end`位置,跳出循环,`end--` | ||
|
||
4. 第四轮 | ||
|
||
| 3 | 4 | 5 | 6 | 8 | | ||
| --- | ---- | --- | --- | --- | | ||
| cur | next | | | | | ||
|
||
`arr[cur] < arr[next]`,不交换位置 | ||
|
||
| 3 | 4 | 5 | 6 | 8 | | ||
| --- | --- | --- | --- | --- | | ||
| | cur | | | | | ||
|
||
`cur`位于`end`位置,跳出循环。 | ||
|
||
这一轮`flag = false`,说明数组已经有序,提前结束排序 | ||
|
||
## 冒泡排序代码实现 | ||
|
||
```java | ||
public void bubbleSort(int[] nums) { | ||
int n = nums.length; | ||
for (int i = 0; i < n - 1; i++) { | ||
boolean flag = false; | ||
for (int j = 0; j < n - i - 1; j++) { | ||
if (nums[j] > nums[j + 1]) { | ||
int temp = nums[j]; | ||
nums[j] = nums[j + 1]; | ||
nums[j + 1] = temp; | ||
// 支持元祖的编程语言可以写成 (nums[j], nums[j + 1]) = (nums[j + 1], nums[j]); | ||
flag = true; | ||
} | ||
} | ||
if (!flag) { | ||
break; | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
**本模块是公共类课程,适合绝大部分计算机岗位。由于 Java 受众最广且易读性强,本模块示例代码使用 Java 编写,其他语言也可参考学习。** | ||
|
||
## 选择排序原理 | ||
|
||
选择排序是一种简单的排序算法,主要思想是每次从未排序的元素中选择最小或最大的元素,放到已排序的末尾。 | ||
|
||
选择排序的时间复杂度是 O(n^2),空间复杂度是 O(1)。 | ||
|
||
## 选择排序实现 | ||
|
||
如,对数组`arr = [5, 3, 8, 6, 4]`进行升序选择排序。 | ||
|
||
先让指针`i`指向数组第 0 个元素,表示未排序的第 0 个元素,每经过一轮选择,`i++`。 | ||
|
||
每一轮让指针`minIndex`指向`i`,然后依次比较,找到最小的元素的索引,最后和`i`指向的元素交换位置。 | ||
|
||
排序过程如下: | ||
|
||
1. 第一轮 | ||
|
||
| 5 | 3 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| min | j | | | | | ||
|
||
`arr[min] > arr[j]`,改变指针指向 | ||
|
||
| 5 | 3 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| i | min | j | | | | ||
|
||
`arr[min] < arr[j]`,不改变指针指向 | ||
|
||
| 5 | 3 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| i | min | | j | | | ||
|
||
`arr[min] < arr[j]`,不改变指针指向 | ||
|
||
| 5 | 3 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| i | min | | | j | | ||
|
||
`arr[min] < arr[j]`,不改变指针指向,此时`j`位于数组末尾,开始交换位置 | ||
|
||
| 3 | 5 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
|
||
2. 第二轮 | ||
|
||
| 3 | 5 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| | min | j | | | | ||
|
||
`arr[min] < arr[j]`,不改变指针指向 | ||
|
||
| 3 | 5 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| | min | | j | | | ||
|
||
`arr[min] < arr[j]`,不改变指针指向 | ||
|
||
| 3 | 5 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| | min | | | j | | ||
|
||
`arr[min] > arr[j]`,改变指针指向,此时`j`位于数组末尾,开始交换位置 | ||
|
||
| 3 | 4 | 8 | 6 | 5 | | ||
| --- | --- | --- | --- | --- | | ||
|
||
3. 第三轮 | ||
|
||
| 3 | 4 | 8 | 6 | 5 | | ||
| --- | --- | --- | --- | --- | | ||
| | | min | j | | | ||
|
||
`arr[min] > arr[j]`,改变指针指向 | ||
|
||
| 3 | 4 | 8 | 6 | 5 | | ||
| --- | --- | --- | --- | --- | | ||
| | | i | min | j | | ||
|
||
`arr[min] > arr[j]`,改变指针指向,此时`j`位于数组末尾,开始交换位置 | ||
|
||
| 3 | 4 | 5 | 6 | 8 | | ||
| --- | --- | --- | --- | --- | | ||
|
||
4. 第四轮 | ||
|
||
| 3 | 4 | 5 | 6 | 8 | | ||
| --- | --- | --- | --- | --- | | ||
| | | | min | j | | ||
|
||
`arr[min] < arr[j]`,不改变指针指向,此时`j`位于数组末尾,开始交换位置 | ||
|
||
循环结束后,`i`指向数组末尾,排序完成。 | ||
|
||
## 选择排序代码实现 | ||
|
||
```java | ||
public void selectionSort(int[] nums) { | ||
int n = nums.length; | ||
for (int i = 0; i < n - 1; i++) { | ||
int minIndex = i; | ||
for (int j = i + 1; j < n; j++) { | ||
if (nums[j] < nums[minIndex]) { | ||
minIndex = j; | ||
} | ||
} | ||
if (minIndex != i) { | ||
int temp = nums[i]; | ||
nums[i] = nums[minIndex]; | ||
nums[minIndex] = temp; | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
**本模块是公共类课程,适合绝大部分计算机岗位。由于 Java 受众最广且易读性强,本模块示例代码使用 Java 编写,其他语言也可参考学习。** | ||
|
||
## 插入排序原理 | ||
|
||
插入排序是一种简单的排序算法,主要思想是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。 | ||
|
||
插入排序的时间复杂度是 O(n^2),空间复杂度是 O(1)。 | ||
|
||
## 插入排序实现 | ||
|
||
如,对数组 `arr = [5, 3, 8, 6, 4]` 进行升序插入排序。 | ||
|
||
默认第 0 个元素是有序的, | ||
|
||
先让指针 `i` 指向数组第 1 个元素,表示未排序区域的第 0 个元素,每经过一轮插入,`i++`。 | ||
|
||
用`temp`保存`i`指向的元素。 | ||
|
||
每一轮让指针 `j` 指向 `i - 1`,然后依次向前比较,找到合适的位置插入当前元素。 | ||
|
||
排序过程如下: | ||
|
||
1. 第一轮 | ||
|
||
`temp = arr[1] = 3` | ||
|
||
| 5 | | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| j | i | | | | | ||
|
||
`arr[j] > temp`,`arr[j]` 向后移动 | ||
|
||
| | 5 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| | i | | | | | ||
|
||
此时 `j = -1`,直接在数组头部插入 `temp` | ||
|
||
| 3 | 5 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
|
||
2. 第二轮 | ||
|
||
`temp = arr[2] = 8` | ||
|
||
| 3 | 5 | | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| | j | i | | | | ||
|
||
`arr[j] < temp`,直接插入 `temp` | ||
|
||
| 3 | 5 | 8 | 6 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
|
||
3. 第三轮 | ||
|
||
`temp = arr[3] = 6` | ||
|
||
| 3 | 5 | 8 | | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| | | j | i | | | ||
|
||
`arr[j] > temp`,`arr[j]` 向后移动 | ||
|
||
| 3 | 5 | | 8 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
| | j | | i | | | ||
|
||
`arr[j] < temp`,直接插入 `temp` | ||
|
||
| 3 | 5 | 6 | 8 | 4 | | ||
| --- | --- | --- | --- | --- | | ||
|
||
4. 第四轮 | ||
|
||
`temp = arr[4] = 4` | ||
|
||
| 3 | 5 | 6 | 8 | | | ||
| --- | --- | --- | --- | --- | | ||
| | | | j | i | | ||
|
||
`arr[j] > temp`,`arr[j]` 向后移动 | ||
|
||
| 3 | 5 | 6 | | 8 | | ||
| --- | --- | --- | --- | --- | | ||
| | | j | | i | | ||
|
||
`arr[j] > temp`,`arr[j]` 向后移动 | ||
|
||
| 3 | 5 | | 6 | 8 | | ||
| --- | --- | --- | --- | --- | | ||
| | j | | | i | | ||
|
||
`arr[j] > temp`,`arr[j]` 向后移动 | ||
|
||
| 3 | | 5 | 6 | 8 | | ||
| --- | --- | --- | --- | --- | | ||
| j | | | | i | | ||
|
||
`arr[j] < temp`,直接插入 `temp` | ||
|
||
| 3 | 4 | 5 | 6 | 8 | | ||
| --- | --- | --- | --- | --- | | ||
|
||
`i` 已经到达数组末尾,排序结束。 | ||
|
||
## 插入排序代码实现 | ||
|
||
```java | ||
public void insertionSort(int[] nums) { | ||
int n = nums.length; | ||
for (int i = 1; i < n; i++) { | ||
int key = nums[i]; | ||
int j = i - 1; | ||
while (j >= 0 && nums[j] > key) { | ||
nums[j + 1] = nums[j]; | ||
j--; | ||
} | ||
nums[j + 1] = key; | ||
} | ||
} | ||
``` |
Oops, something went wrong.