当前位置:网站首页>排序:归并排序和快速排序
排序:归并排序和快速排序
2022-07-26 07:20:00 【李小恩恩子】
力扣leetcode:912.排序数组,可以用来练习排序算法
题目:给你一个整数数组 nums,请你将该数组升序排列。
目录
归并和快排都运用了递归和分治的思想。归并:二叉树的后续遍历;快排:二叉树的前序遍历。
为什莫这么说呢?排序的核心代码框架:
归并排序
public void sort(int[] nums,int low,int high){
if(low == high) return;
int mid = low + (high - low)/2;
//对左半数组排序,nums[low...mid]
sort(nums,low,mid);
//对右半边数组排序,nums[mid+1...high]
sort(nums,mid+1,high);
//二叉树遍历的后续位置,合并nums[low...mid]和nums[mid+1...high]
merge(nums,low,mid,high);
}先对左右数组进行排序,再合并。
快速排序
public void sort(int[] nums,int low,int high){
if(low >= high) return;
//二叉树的前序位置,进行划分
int p = partition(nums,low,high);
//对划分的左半nums[low...p-1]进行排序
sort(nums,low,p - 1);
//对划分的右半nums[p+1...high]进行排序
sort(nums,p + 1,high);
} 先构造分界点,然后去左右子树组构造分界点。
归并排序
过程:
- 先将要进行排序的数组分为两部分Left和Right
- 对Left部分排序
- 对Right部分排序
- 合并Left和Right两部分,合并成一个有序数组
代码:
class Solution {
int[] temp; //归并排序的辅助数组
public int[] sortArray(int[] nums) {
//归并
temp = new int[nums.length];
sort(nums,0,nums.length-1);
return nums;
}
public void sort(int[] nums,int low,int high){
if(low == high) return;
//有效防止溢出,其实和(low + high)/2等价
int mid = low + (high - low)/2;
//对左半数组排序
sort(nums,low,mid);
//对右半边数组排序
sort(nums,mid+1,high);
merge(nums,low,mid,high);
}
public void merge(int[] nums,int low,int mid,int high){
//先将nums[low...high]的值存入辅助数组temp中
for(int i = low;i <= high;i++){
temp[i] = nums[i];
}
//双指针合并
int i = low,j = mid + 1;
for(int m = low; m <= high; m++){
if(i == mid + 1) nums[m] = temp[j++];
else if( j == high + 1) nums[m] = temp[i++];
else if(temp[i] > temp[j]) nums[m] = temp[j++];
else nums[m] = temp[i++];
}
}
}快速排序
快速排序是先将一个元素排好序,再将剩下的元素排好序。核心函数是partition函数:
过程:
- 是在nums[low...high]中寻找一个分界点p
- 交换元素使得nums[low...p-1]的元素都小于等于nums[p]
- nums[p+1...high]的元素都大于nums[p]
partition其实就是把nums[p]这个元素排好序了

代码:
class Solution {
public int[] sortArray(int[] nums) {
//快排
sort(nums,0,nums.length - 1);
return nums;
}
public void sort(int[] nums,int low,int high){
if(low >= high) return;
//对nums[low...high]进行划分
//使得nums[low...p-1] <= nums[p] < nums[p+1...high]
int p = partition(nums,low,high);
sort(nums,low,p - 1);
sort(nums,p + 1,high);
}
public int partition(int[] nums,int low,int high){
int pivot = nums[low];
int i = low + 1,j = high;
while(i <= j){ //不断交换,直到满足左边都小于,右边都大于
while(i < high && nums[i] <= pivot) i++; //该while结束的时候num[i] > pivot
while(j > low && nums[j] > pivot) j--; //该while结束的时候num[j] <= pivot
if(i >= j) break;
swap(nums,i,j); //将这两交换
}
//将pivot放到合适的位置上,pivot左边的元素比它小,pivot右边的元素比它大
swap(nums,low,j);
return j;
}
public void swap(int[] nums,int a,int b){
int temp = nums[b];
nums[b] = nums[a];
nums[a] = temp;
}
}
边栏推荐
- Image annotation software reference
- Deep learning learning notes -- solve the problem of slow download of CONDA and pip
- Why can't extern compile variables decorated with const?
- 倒计时2日!基于 Apache DolphinScheduler&TiDB 的交叉开发实践,从编写到调度让你大幅提升效率
- Precious metal knowledge: lethal short-term secret script
- College degree sales career, from the third tier 4K to the first tier 20k+, I am very satisfied with myself
- C51 and MDK coexist keil5 installation tutorial
- QT: list box, table, tree control
- ModuleNotFoundError: No module named ‘pip‘解决办法
- Uncover the mystery of cloud native data management: operation level
猜你喜欢

达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程【三】

Check the top 10 best graphics software of the year, meet 99% of your chart needs, and collect it quickly

NPM command

Anaconda installation tutorial - hands on installation

Opencv learning warp Perspective

PXE高效批量网络装机

C51与MDK共存 Keil5安装教程

中国联通改造 Apache DolphinScheduler 资源中心,实现计费环境跨集群调用与数据脚本一站式访问

6. Backup and recovery of MySQL database

NFT digital collection system development: activating digital cultural heritage
随机推荐
A guide for you to fully use TS
C51 and MDK coexist keil5 installation tutorial
HCIP---MPLS详解和BGP路由过滤
Relevant configurations of pychart: change font style and size, change picture background, and change the font color of console output
NFT数字藏品系统开发:数字藏品赋予品牌新活力
DaemonSet
Leetcode:749. isolate virus [Unicom component + priority queue + status representation]
6、MySQL数据库的备份与恢复
anaconda安装教程-手把手教你安装
hot100 哈希
How to delete a statement audit log?
[C language] do you really know printf? (printf is typically error prone, and collection is strongly recommended)
Modulenotfounderror: no module named 'pip' solution
It's another summer of open source. 12000 project bonuses are waiting for you!
With Huawei cloud welink, you can connect to the world even in the countryside
NFT数字藏品系统开发:NFT数藏 的最佳数字营销策略有哪些
又是一年开源之夏,1.2万项目奖金等你来拿!
Singles cup web WP
pycharm常用快捷键
QT: modal, modeless, text box, button, single line input box