算法通关村第10关【青铜】| 快速排序各种写法

news/2025/2/22 3:34:59/

思路:

指定一个数字,将数组比他小的放到左边,比他大的放到右边,实现归位

然后再指定一个数字递归,一直遍历完数组

最好的情况每次指定的都是中间位置的数字,划分完后两边长度相等,2T(n/2) + O(n),复杂度O(nlog(n))

可以证明,平均情况下的时间复杂度也是O(nlog(n))

最坏的情况,每次指定的都是最小的数字,n的复杂度归位,一共n次,T(n-1) + O(n),复杂度O(n^2)

方式一、对撞型指针+指定头元素

class Solution {public int[] sortArray(int[] nums) {trace(nums,0,nums.length - 1);return nums;}public void trace(int[] nums,int start,int end) {if(start>=end){return;}int p = nums[start];int l = start;int r = end;while(l<r){while(nums[r]>=p&&l<r){r--;}nums[l] = nums[r];while(nums[l]<=p&&l<r){l++;}nums[r] = nums[l];}nums[l] = p;trace(nums,l + 1,end);trace(nums,start,l-1);}}

方式二、对撞型指针+指定中间

这里要注意的细节很多,判断条件的时候nums[l]>p不能等于,不然左指针会跑到右边去同理右边

同时l<=r,不能是小于

class Solution {public int[] sortArray(int[] nums) {trace(nums,0,nums.length - 1);return nums;}public void trace(int[] nums,int start,int end) {if(start>=end){return;}int p = nums[(start+end)/2];int l = start;int r = end;while(l<=r){while(nums[r]>p&&l<=r){r--;}while(nums[l]<p&&l<=r){l++;}if(l<=r){swap(nums,l,r);l++;r--;}}trace(nums,start,r);trace(nums,l,end);}private void swap(int[] nums, int i, int j) {int temp = nums[i];nums[i] = nums[j];nums[j] = temp;}}

方式三、快慢指针+指定尾元素

lass Solution {public int[] sortArray(int[] nums) {partition(nums, 0, nums.length - 1);return nums;}public void partition(int[] nums, int l, int r) {if(l<r){int pivot = nums[r];int i = l - 1;//快慢指针找到目标位置for (int j = l; j <= r - 1; ++j) {if (nums[j] <= pivot) {i = i + 1;swap(nums, i, j);}}//放置目标元素swap(nums, i + 1, r);int p = i+1;//递归partition(nums,l,p-1);partition(nums,p+1,r);}}private void swap(int[] nums, int i, int j) {int temp = nums[i];nums[i] = nums[j];nums[j] = temp;}
}


http://www.ppmy.cn/news/1079702.html

相关文章

Oracle Merge Into ORA-00001: unique constaint violated问题

最近使用Datax同步进行定时数据同步&#xff0c;并在同步完之后进行回调sql进行统计操作。对应的ORACLE表结构如下&#xff1a; create table DATA_STAT_DAY ( DATA_DATE DATE, ID VARCHAR2(2), NAME VARCHAR2(2), CLASSNO VARCHAR2(2), SCORES NUMBER(16,0) );CREATE UNIQU…

qt 信号与槽机制,登录界面跳转

登录界面跳转 配置文件 .pro QT core gui texttospeechgreaterThan(QT_MAJOR_VERSION, 4): QT widgetsCONFIG c11# The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # d…

FFplay播放avsync学习

FFplay有三种同步方式 命令行通过option sync参数可以设置同步方式 sync参数取值范围为&#xff1a;audio/video/ext audio以音频时钟为主时钟&#xff0c;默认方式 以音频为主时钟的逻辑&#xff0c;拉长或者缩短视频帧的显示时长&#xff0c;或者丢弃视频帧。 video以视…

【Cadence】Calculator计算sp的3dB带宽

【Cadence】Calculator计算sp的3dB带宽 1.计算最大增益2.cross函数3. 3dB带宽 下面演示如何在Cadence计算s参数&#xff08;如增益&#xff09;的3dB带宽 1.计算最大增益 ymax函数 2.cross函数 cross函数可以计算经过y轴给定值对应的x坐标 edge number选择1是经过的第一个点…

解决uView2 SwipeAction滑动单元格,点击按钮不关闭的问题

1、首先找到安装SwipeAction插件的位置&#xff0c;我的安装在一下位置 2.在 u-swipe-action-item.vue 文件内找到buttonClickHandler方法 3.在buttonClickHandler方法内添加代码 this.status close

传输层—UDP原理详解

目录 前言 1.netstat 2.pidof 3.UDP协议格式 4.UDP的特点 5.面向数据报 6.UDP的缓冲区 7.UDP使用注意事项 8.基于UDP的应用层协议 总结 前言 在之前的文章中为大家介绍了关于网络协议栈第一层就是应用层&#xff0c;包含套接字的使用&#xff0c;在应用层编码实现服务…

智能电话机器人的出现,能够解决哪些问题?

经济的繁荣与高速的发展&#xff0c;使得电销这个方式快速地融合在房地产与金融投资等大部分行业上。在电销人员与客户的沟通上&#xff0c;难免会出现很多问题&#xff0c;毕竟所面对的客户都是各行各业&#xff0c;他们有着不同的经历和身份。 对于时常需要处理客户投诉、安…

Jenkins上使用expect脚本实现发布物上传SVN遇到字符集问题

Jenkins上使用expect脚本实现发布物上传SVN遇到字符集问题 编写一个expect脚本实现发布物上传到svn的功能比较简单&#xff0c;可能需要注意一点就是增加文件替换功能&#xff0c;其核心步骤有4步&#xff1a; #从SVN指定路径下载内容 spawn svn co "$svndir" #先删…