public class QuickSort {
public static void splitNum(int[] array){
int cur = 0;
int lessIndex = 0;
int len = array.length;
while (cur < len) {
if(array[cur] <= array[len-1]) {
swap(array,cur,lessIndex++);
}
cur++;
}
}
public static void splitNum2(int[] array){
int cur = 0;
int lessIndex = -1;
int len = array.length;
int bigIndex = len - 1;
while (cur < bigIndex) {
if(array[cur] < array[len-1]) {
swap(array,cur++,++lessIndex);
} else if(array[cur] > array[len-1]){
swap(array,cur,--bigIndex);
} else {
cur++;
}
}
swap(array,bigIndex,len-1);
}
public static void quickSort(int[] array){
if(array == null || array.length < 2) {
return;
}
process(array,0,array.length-1);
}
public static void process(int[] array,int left,int right){
if(left >= right) {
return;
}
int[] partition = partition(array, left, right);
process(array,left,partition[0]-1);
process(array,partition[1]+1,right);
}
public static int[] partition(int[] array,int left,int right){
int lessIndex = left - 1;
int bigIndex = right;
while (left < bigIndex) {
if(array[left] < array[right]) {
swap(array,left++,++lessIndex);
} else if(array[left] > array[right]){
swap(array,left,--bigIndex);
} else {
left++;
}
}
swap(array,bigIndex,right);
return new int[]{lessIndex+1,bigIndex};
}
public static void swap(int[] array, int i, int j){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void quickSortByStack(int[] array){
if(array == null || array.length < 2) {
return;
}
Job job = new Job(0,array.length-1);
Stack<Job> stack = new Stack<>();
stack.push(job);
while (!stack.isEmpty()) {
Job exeuteJob = stack.pop();
int[] partition = partition(array, exeuteJob.left, exeuteJob.right);
if(partition[0] > exeuteJob.left) {
stack.push(new Job(exeuteJob.left,partition[0]-1));
}
if(partition[1] < exeuteJob.right) {
stack.push(new Job(partition[0]+1,exeuteJob.right));
}
}
}
public static class Job{
int left;
int right;
public Job(int left,int right){
this.left = left;
this.right = right;
}
}
public static void main(String[] args) {
ArrayUtil.testArraySorted(1000000,50,50,QuickSort.class,"quickSortByStack");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本