algorithm-base/animation-simulation/数据结构和算法/快速排序.md

495 lines
20 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

> **[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>进入。
###
1.
2.
3.
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/微信截图_20210214212239.5rco4z7idoc0.png)
[3,1,2] [7,6,5,8] 1
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/微信截图_20210218143843.7l004l5cya00.png)
O(nlogn)
nums[high] high high low
便
![](https://img-blog.csdnimg.cn/20210317190017344.gif)
Java Code:
```java
class Solution {
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指针
while (low < high && nums[high] >= pivot) {
high--;
}
//填坑
if (low < high) nums[low] = nums[high];
while (low < high && nums[low] <= pivot) {
low++;
}
//填坑
if (low < high) nums[high] = nums[low];
}
//基准数放到合适的位置
nums[low] = pivot;
return low;
}
}
```
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/微信截图_20210218153208.5wn3lgpbljg0.png)
low pivot hight pivot 使
![](https://img-blog.csdnimg.cn/20210317190153677.gif#pic_center)
AC
```java
class Solution {
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];
int start = low;
while (low < 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,start,low);
return low;
}
public void swap (int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
```
****
n O(nlogn)
[1,2,3,4] [4,3,2,1] n-1 退 O(n^2)
****
使O (logn), n-1 O(n).
****
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/微信截图_20210218165440.17ovoc8246gw.png)
使 1 1
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/微信截图_20210218170306.6oe0mbb5gr40.png)
****
```java
class Solution {
public int[] sortArray(int[] nums) {
Stack<Integer> stack = new Stack<>();
stack.push(nums.length - 1);
stack.push(0);
while (!stack.isEmpty()) {
int low = stack.pop();
int high = stack.pop();
if (low < high) {
int index = partition(nums, low, high);
stack.push(index - 1);
stack.push(low);
stack.push(high);
stack.push(index + 1);
}
}
return nums;
}
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[low] <= pivot) low++;
if (low >= high) break;
swap(nums, low, high);
}
swap(nums,start,low);
return low;
}
public void swap (int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
```
****
****
nums[low]
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/微信截图_20210218172418.338n8fxuqka0.png)
2 7 7 pivot
nums[low] 使
```java
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);
```
nums[low] nums[high], nums[mid], [4,2,3] [3,2,4]. 3
****
```java
class Solution {
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 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);
//下面和之前一样,仅仅是多了上面几行代码
int pivot = nums[low];
int start = low;
while (low < 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,start,low);
return low;
}
public void swap (int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
```
**使**
使使 7
**+**
```java
class Solution {
private static final int INSERTION_SORT_MAX_LENGTH = 7;
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);
}
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);
int pivot = nums[low];
int start = low;
while (low < 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,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;
for (j = i-1; j >= 0; --j) {
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;
}
}
```
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/微信截图_20210220134731.3i6nhxhivo80.png)
[2,3,6,3,1,6] [8,6]
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/微信截图_20210220134928.1sx24i3fkc8w.png)
![](https://img-blog.csdnimg.cn/20210317190456320.gif#pic_center)
i pivot right right pivot right-- nums[i] i nums[i] < pivot left left povit left++,i++, nums[i] == pivot i++
**++**
Java Code:
```java
class Solution {
private static final int INSERTION_SORT_MAX_LENGTH = 7;
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 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);
//三向切分
int left = low, i = low + 1, right = high;
int pvoit = nums[low];
while (i <= right) {
if (pvoit < nums[i]) {
swap(nums,i,right);
right--;
} else if (pvoit == nums[i]) {
i++;
} else {
swap(nums,left,i);
left++;
i++;
}
}
quickSort(nums,low,left-1);
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;
for (j = i-1; j >= 0; --j) {
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;
}
}
```
Python Code:
```python
from typing import List
class Solution:
INSERTION_SORT_MAX_LENGTH = 7
def sortArray(self, nums: List[int])->List[int]:
self.quickSort(nums, 0, len(nums) - 1)
return nums
def quickSort(self, nums: List[int], low: int, high: int):
#
if high - low <= self.INSERTION_SORT_MAX_LENGTH:
self.insertSort(nums, low, high)
return
#
mid = low + ((high - low) >> 1)
if nums[low] > nums[high]:
self.swap(nums, low, high)
if nums[mid] > nums[high]:
self.swap(nums, mid, high)
if nums[mid] > nums[low]:
self. swap(nums, mid, low)
#
left = low
i = low + 1
right = high
pivot = nums[low]
while i <= right:
if pivot < nums[i]:
self.swap(nums, i, right)
right -= 1
elif pivot == nums[i]:
i += 1
else:
self.swap(nums, left, i)
left += 1
i += 1
self.quickSort(nums, low, left - 1)
self.quickSort(nums, right + 1, high)
def insertSort(self, nums: List[int], low: int, high: int):
for i in range(low + 1, high + 1):
temp = nums[i]
j = i - 1
while j >= 0:
if temp < nums[j]:
nums[j + 1] = nums[j]
j -= 1
continue
break
nums[j + 1] = temp
def swap(self, nums: List[int], i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```