快速排序,简而言之就是不断的切割在排序,找个基数,不断的与原数列进行对比,比之大的放在后面指针前移,比之小的放在前面指针后移,元素交换; 最后前后指针重合,重合位置以此分割,排序,再次重复之前的步骤;
import java.util.Arrays;
import java.util.Scanner;
public class quicksort {
public static void main(String[] args) {
int[] nums = {9,5,7,6,0,4,3,2,1};
quickSort(nums,0, nums.length-1);
System.out.println(Arrays.toString(nums));
}
public static void quickSort(int[] nums,int left,int right){
if(left>right)return;
int i=left;
int j=right;
int basic=nums[left];
while(i<j) {
while(i<j&&basic<=nums[j])j--; //比基数大,指针前移
while(i<j&&basic>=nums[i])i++; //比基数小,指针后移
int temp=nums[i]; //交换
nums[i]=nums[j];
nums[j]=temp;
}
nums[left]=nums[i]; //移动基数
nums[i]=basic;
quickSort(nums,left,i-1);
quickSort(nums,i+1,right);
}
}