免费企业网站建设技术,成都个人建网站,与客户沟通网站建设的技巧,市民专线是什么大家好我是苏麟,今天带来快速排序 . 快速排序
单边快速排序(lomuto 洛穆托分区方案)
单边循环 (lomuto分区) 要点 :
选择最右侧元素作为基准点j 找比基准点小的#xff0c;i 找比基准点大的#xff0c;一旦找到#xff0c;二者进行交换。
交换时机: 找到小的#xff0c…大家好我是苏麟,今天带来快速排序 . 快速排序
单边快速排序(lomuto 洛穆托分区方案)
单边循环 (lomuto分区) 要点 :
选择最右侧元素作为基准点j 找比基准点小的i 找比基准点大的一旦找到二者进行交换。
交换时机: 找到小的且与i不相等oi找到 基准点元素后不应自增
最后基准点与i 交换i 即为基准点最终索引
B站解析 : 基础算法-210-排序算法-单边快排_哔哩哔哩_bilibili 代码 :
class Solution {public int[] sortArray(int[] nums) {int length nums.length;sort(nums,0,length - 1);return nums;}public void sort(int[] nums,int left,int right){if(left right){return;}int i qicke(nums,left,right);sort(nums,left,i - 1);sort(nums,i 1,right);}public int qicke(int[] nums,int left,int right){int i left;int j left;int p nums[right];while(j right){if(nums[j] p){if(i ! j){swap(nums,i,j);}i;}j;}swap(nums,i,right);return i; }public void swap(int[]nums,int i,int j){int temp nums[i];nums[i]nums[j];nums[j]temp;}
}
双边快速排序
双边循环要点 :
选择最左侧元素作为基准点 找比基准点小的i 找比基准点大的一旦找到二者进行交换
i从左向右j从右向左
最后基准点与i 交换i 即为基准点最终索引
B站解析 : 基础算法-211-排序算法-双边快排_哔哩哔哩_bilibili 解析 :
class Solution {public int[] sortArray(int[] nums) {int length nums.length;sort(nums,0,length - 1);return nums;}public void sort(int[] nums,int left,int right){if(left right){return;}int i qicke(nums,left,right);sort(nums,left,i - 1);sort(nums,i 1,right);}public int qicke(int[] nums,int left,int right){int i left;int j right;int p nums[left];while(i j){while(i j nums[j] p){j--;}while(i j nums[i] p){i;}swap(nums,i,j);}swap(nums,i,left);return i; }public void swap(int[]nums,int i,int j){int temp nums[i];nums[i]nums[j];nums[j]temp;}
} 小题一道
这道题是一个数组排序题目 , 没有指定什么排序 , 但是为了更好的学习快速排序 ,请大家用快速排序做这道题 , 但是有一个Bug 有的块排会超时间限制 , 请大家自己思考用什么样的快排 .
题目 :
LeetCode : 912 排序数组
912. 排序数组 分析 :
根据上面写出快排
解析 :
class Solution {public int[] sortArray(int[] nums) {int length nums.length;quickSort(nums,0,length - 1);return nums;}public void quickSort(int[] array,int start,int end){if (start end) {return; } int left start, right end; int pivot array[(start end) / 2];while (left right) {while (left right array[left] pivot){left;}while (left right array[right] pivot){ right--; }if (left right){ int temp array[left]; array[left] array[right]; array[right] temp; left;right--; }} quickSort(array, start, right); quickSort(array, left, end);}
}
这期就到这里 , 下期见! 文章转载自: http://www.morning.mgmyt.cn.gov.cn.mgmyt.cn http://www.morning.hjlsll.com.gov.cn.hjlsll.com http://www.morning.rhgtc.cn.gov.cn.rhgtc.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.ldgqh.cn.gov.cn.ldgqh.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.nqbkb.cn.gov.cn.nqbkb.cn http://www.morning.pqjpw.cn.gov.cn.pqjpw.cn http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.c7622.cn.gov.cn.c7622.cn http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.xnpj.cn.gov.cn.xnpj.cn http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn http://www.morning.xkjrq.cn.gov.cn.xkjrq.cn http://www.morning.lmrcq.cn.gov.cn.lmrcq.cn http://www.morning.ahscrl.com.gov.cn.ahscrl.com http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.lltdf.cn.gov.cn.lltdf.cn http://www.morning.skrww.cn.gov.cn.skrww.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn http://www.morning.bsrqy.cn.gov.cn.bsrqy.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.wrlxy.cn.gov.cn.wrlxy.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.jtszm.cn.gov.cn.jtszm.cn http://www.morning.lngyd.cn.gov.cn.lngyd.cn http://www.morning.dnvhfh.cn.gov.cn.dnvhfh.cn http://www.morning.wdnkp.cn.gov.cn.wdnkp.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn http://www.morning.wqpm.cn.gov.cn.wqpm.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.c7496.cn.gov.cn.c7496.cn http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.mfsjn.cn.gov.cn.mfsjn.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.lpnpn.cn.gov.cn.lpnpn.cn http://www.morning.kstgt.cn.gov.cn.kstgt.cn http://www.morning.pdxqk.cn.gov.cn.pdxqk.cn http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.kkqgf.cn.gov.cn.kkqgf.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.nkddq.cn.gov.cn.nkddq.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.fdfsh.cn.gov.cn.fdfsh.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.jbpodhb.cn.gov.cn.jbpodhb.cn http://www.morning.jwlmm.cn.gov.cn.jwlmm.cn http://www.morning.rxrw.cn.gov.cn.rxrw.cn http://www.morning.rwlns.cn.gov.cn.rwlns.cn http://www.morning.pqndg.cn.gov.cn.pqndg.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.qglqb.cn.gov.cn.qglqb.cn http://www.morning.ybgyz.cn.gov.cn.ybgyz.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.rhzzf.cn.gov.cn.rhzzf.cn