mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-11 04:14:41 +00:00
代码重构 【Github Actions】
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
### 快速排序
|
||||
|
||||
@@ -50,23 +50,23 @@ Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int[] sortArray(int[] nums) {
|
||||
|
||||
public int[] sortArray(int[] nums) {
|
||||
|
||||
quickSort(nums,0,nums.length-1);
|
||||
return nums;
|
||||
|
||||
|
||||
}
|
||||
public void quickSort (int[] nums, int low, int high) {
|
||||
|
||||
|
||||
if (low < high) {
|
||||
int index = partition(nums,low,high);
|
||||
quickSort(nums,low,index-1);
|
||||
quickSort(nums,index+1,high);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public int partition (int[] nums, int low, int high) {
|
||||
|
||||
|
||||
int pivot = nums[low];
|
||||
while (low < high) {
|
||||
//移动hight指针
|
||||
@@ -84,7 +84,7 @@ class Solution {
|
||||
//基准数放到合适的位置
|
||||
nums[low] = pivot;
|
||||
return low;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -102,11 +102,11 @@ class Solution {
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int[] sortArray (int[] nums) {
|
||||
|
||||
public int[] sortArray (int[] nums) {
|
||||
|
||||
quickSort(nums,0,nums.length-1);
|
||||
return nums;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void quickSort (int[] nums, int low, int high) {
|
||||
@@ -115,30 +115,30 @@ class Solution {
|
||||
int index = partition(nums,low,high);
|
||||
quickSort(nums,low,index-1);
|
||||
quickSort(nums,index+1,high);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int partition (int[] nums, int low, int high) {
|
||||
|
||||
|
||||
int pivot = nums[low];
|
||||
int start = low;
|
||||
|
||||
|
||||
while (low < high) {
|
||||
while (low < high && nums[high] >= pivot) high--;
|
||||
while (low < high && nums[high] >= pivot) high--;
|
||||
while (low < high && nums[low] <= pivot) low++;
|
||||
if (low >= high) break;
|
||||
swap(nums, low, high);
|
||||
swap(nums, low, high);
|
||||
}
|
||||
//基准值归位
|
||||
swap(nums,start,low);
|
||||
return low;
|
||||
}
|
||||
public void swap (int[] nums, int i, int j) {
|
||||
}
|
||||
public void swap (int[] nums, int i, int j) {
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -150,7 +150,7 @@ class Solution {
|
||||
|
||||
**快速排序的空间复杂度分析**
|
||||
|
||||
快速排序主要时递归造成的栈空间的使用,最好情况时其空间复杂度为O (logn),对应递归树的深度。最坏情况时则需要 n-1 次递归调用,此时空间复杂度为O(n).
|
||||
快速排序主要时递归造成的栈空间的使用,最好情况时其空间复杂度为 O (logn),对应递归树的深度。最坏情况时则需要 n-1 次递归调用,此时空间复杂度为 O(n).
|
||||
|
||||
**快速排序的稳定性分析**
|
||||
|
||||
@@ -158,7 +158,7 @@ class Solution {
|
||||
|
||||

|
||||
|
||||
此时无论使用哪一种方法,第一次排序后,黄色的 1 都会跑到红色 1 的前面,所以快速排序是不稳定的排序算法。
|
||||
此时无论使用哪一种方法,第一次排序后,黄色的 1 都会跑到红色 1 的前面,所以快速排序是不稳定的排序算法。
|
||||
|
||||

|
||||
|
||||
@@ -170,7 +170,7 @@ class Solution {
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
|
||||
|
||||
public int[] sortArray(int[] nums) {
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
stack.push(nums.length - 1);
|
||||
@@ -191,25 +191,25 @@ class Solution {
|
||||
}
|
||||
|
||||
public int partition (int[] nums, int low, int high) {
|
||||
|
||||
|
||||
int pivot = nums[low];
|
||||
int start = low;
|
||||
while (low < high) {
|
||||
|
||||
while (low < high && nums[high] >= pivot) high--;
|
||||
while (low < high && nums[high] >= pivot) high--;
|
||||
while (low < high && nums[low] <= pivot) low++;
|
||||
if (low >= high) break;
|
||||
swap(nums, low, high);
|
||||
swap(nums, low, high);
|
||||
}
|
||||
swap(nums,start,low);
|
||||
return low;
|
||||
|
||||
}
|
||||
public void swap (int[] nums, int i, int j) {
|
||||
}
|
||||
public void swap (int[] nums, int i, int j) {
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -231,7 +231,7 @@ class Solution {
|
||||
int mid = low + ((high-low) >> 1);
|
||||
if (nums[low] > nums[high]) swap(nums,low,high);
|
||||
if (nums[mid] > nums[high]) swap(nums,mid,high);
|
||||
if (nums[mid] > nums[low]) swap(nums,mid,low);
|
||||
if (nums[mid] > nums[low]) swap(nums,mid,low);
|
||||
```
|
||||
|
||||
其含义就是让我们将中间元素放到 nums[low] 位置做为基准值,最大值放到 nums[high],最小值放到 nums[mid],即 [4,2,3] 经过上面代码处理后,则变成了 [3,2,4].此时我们选取 3 做为基准值,这样也就避免掉了选取最大或最小值做为基准值的情况。
|
||||
@@ -240,7 +240,7 @@ class Solution {
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int[] sortArray(int[] nums) {
|
||||
public int[] sortArray(int[] nums) {
|
||||
quickSort(nums,0,nums.length-1);
|
||||
return nums;
|
||||
}
|
||||
@@ -249,7 +249,7 @@ class Solution {
|
||||
int index = partition(nums,low,high);
|
||||
quickSort(nums,low,index-1);
|
||||
quickSort(nums,index+1,high);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int partition (int[] nums, int low, int high) {
|
||||
@@ -257,24 +257,24 @@ class Solution {
|
||||
int mid = low + ((high-low) >> 1);
|
||||
if (nums[low] > nums[high]) swap(nums,low,high);
|
||||
if (nums[mid] > nums[high]) swap(nums,mid,high);
|
||||
if (nums[mid] > nums[low]) swap(nums,mid,low);
|
||||
if (nums[mid] > nums[low]) swap(nums,mid,low);
|
||||
//下面和之前一样,仅仅是多了上面几行代码
|
||||
int pivot = nums[low];
|
||||
int start = low;
|
||||
while (low < high) {
|
||||
while (low < high && nums[high] >= pivot) high--;
|
||||
while (low < high && nums[high] >= pivot) high--;
|
||||
while (low < high && nums[low] <= pivot) low++;
|
||||
if (low >= high) break;
|
||||
swap(nums, low, high);
|
||||
swap(nums, low, high);
|
||||
}
|
||||
swap(nums,start,low);
|
||||
return low;
|
||||
}
|
||||
public void swap (int[] nums, int i, int j) {
|
||||
}
|
||||
public void swap (int[] nums, int i, int j) {
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -287,43 +287,43 @@ class Solution {
|
||||
```java
|
||||
class Solution {
|
||||
private static final int INSERTION_SORT_MAX_LENGTH = 7;
|
||||
|
||||
public int[] sortArray(int[] nums) {
|
||||
|
||||
public int[] sortArray(int[] nums) {
|
||||
quickSort(nums,0,nums.length-1);
|
||||
return nums;
|
||||
}
|
||||
|
||||
|
||||
public void quickSort (int[] nums, int low, int high) {
|
||||
|
||||
|
||||
if (high - low <= INSERTION_SORT_MAX_LENGTH) {
|
||||
insertSort(nums,low,high);
|
||||
return;
|
||||
}
|
||||
}
|
||||
int index = partition(nums,low,high);
|
||||
quickSort(nums,low,index-1);
|
||||
quickSort(nums,index+1,high);
|
||||
quickSort(nums,index+1,high);
|
||||
}
|
||||
|
||||
|
||||
public int partition (int[] nums, int low, int high) {
|
||||
//三数取中,大家也可以使用其他方法
|
||||
int mid = low + ((high-low) >> 1);
|
||||
if (nums[low] > nums[high]) swap(nums,low,high);
|
||||
if (nums[mid] > nums[high]) swap(nums,mid,high);
|
||||
if (nums[mid] > nums[low]) swap(nums,mid,low);
|
||||
if (nums[mid] > nums[low]) swap(nums,mid,low);
|
||||
int pivot = nums[low];
|
||||
int start = low;
|
||||
while (low < high) {
|
||||
while (low < high && nums[high] >= pivot) high--;
|
||||
while (low < high && nums[high] >= pivot) high--;
|
||||
while (low < high && nums[low] <= pivot) low++;
|
||||
if (low >= high) break;
|
||||
swap(nums, low, high);
|
||||
swap(nums, low, high);
|
||||
}
|
||||
swap(nums,start,low);
|
||||
return low;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void insertSort (int[] nums, int low, int high) {
|
||||
|
||||
|
||||
for (int i = low+1; i <= high; ++i) {
|
||||
int temp = nums[i];
|
||||
int j;
|
||||
@@ -331,18 +331,18 @@ class Solution {
|
||||
if (temp < nums[j]) {
|
||||
nums[j+1] = nums[j];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
nums[j+1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void swap (int[] nums, int i, int j) {
|
||||
|
||||
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -362,7 +362,7 @@ class Solution {
|
||||
|
||||

|
||||
|
||||
我们来剖析一下视频,其实原理很简单,我们利用探路指针也就是 i,遇到比 pivot 大的元素,则和 right 指针进行交换,此时 right 指向的元素肯定比 pivot 大,则 right--,但是,此时我们的 nums[i] 指向的元素并不知道情况,所以我们的 i 指针不动,如果此时 nums[i] < pivot 则与 left 指针交换,注意此时我们的 left 指向的值肯定是 等于 povit的,所以交换后我们要 left++,i++, nums[i] == pivot 时,仅需要 i++ 即可,继续判断下一个元素。 我们也可以借助这个思想来解决经典的荷兰国旗问题。
|
||||
我们来剖析一下视频,其实原理很简单,我们利用探路指针也就是 i,遇到比 pivot 大的元素,则和 right 指针进行交换,此时 right 指向的元素肯定比 pivot 大,则 right--,但是,此时我们的 nums[i] 指向的元素并不知道情况,所以我们的 i 指针不动,如果此时 nums[i] < pivot 则与 left 指针交换,注意此时我们的 left 指向的值肯定是 等于 povit 的,所以交换后我们要 left++,i++, nums[i] == pivot 时,仅需要 i++ 即可,继续判断下一个元素。 我们也可以借助这个思想来解决经典的荷兰国旗问题。
|
||||
|
||||
好啦,我们下面直接看代码吧。
|
||||
|
||||
@@ -408,7 +408,7 @@ class Solution {
|
||||
quickSort(nums,right+1,high);
|
||||
}
|
||||
public void insertSort (int[] nums, int low, int high) {
|
||||
|
||||
|
||||
for (int i = low+1; i <= high; ++i) {
|
||||
int temp = nums[i];
|
||||
int j;
|
||||
@@ -416,12 +416,12 @@ class Solution {
|
||||
if (temp < nums[j]) {
|
||||
nums[j+1] = nums[j];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
nums[j+1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void swap (int[] nums, int i, int j) {
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
@@ -432,7 +432,7 @@ class Solution {
|
||||
|
||||
Python Code:
|
||||
|
||||
```python
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
INSERTION_SORT_MAX_LENGTH = 7
|
||||
@@ -489,6 +489,4 @@ class Solution:
|
||||
nums[j] = temp
|
||||
```
|
||||
|
||||
|
||||
好啦,一些常用的优化方法都整理出来啦,还有一些其他的优化算法九数取中,优化递归操作等就不在这里进行描述啦,感兴趣的可以自己看一下。好啦,这期的文章就到这里啦,我们下期见,拜了个拜。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user