快速排序
6.快速排序(Quick Sort)
6.1 算法描述
快速排序的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。
6.2动图演示
6.3代码
public static void quickSort(int[]arr,int start,int end){
int i,j,temp,t;
if (start>end){
return;
}
i=start;
j=end;
//temp就是基准位
temp=arr[start];
while (i<j){
//先看右边,依次往左递减
while ((temp<=arr[j]&&i<j)){
j--;
}
//再看左边,依次往右递增
while (temp>=arr[i]&&i<j){
i++;
}
//如果满足条件则交换
if (i<j){
t=arr[j];
arr[j]=arr[i];
arr[i]=t;
}
}
//最后将基准与i和j相等位置的数字交换
arr[start]=arr[i];
arr[i]=temp;
//递归调用左半数组
quickSort(arr,start,j-1);
//递归调用右半数组
quickSort(arr,j+1,end);
}