Java快速排序
作者:互联网
代码如下
/**
* 快速排序
* @param arr 目标数组
* @param left 排序的起始索引
* @param right 排序的末尾索引
*/
private static void quickSort(int[] arr, int left, int right) {
//递归结束条件
if (left > right) {
return;
}
//基数
int baseNum = arr[left];
//左端索引
int leftIndex = left;
//右端索引
int rightIndex = right;
//循环交换
while (leftIndex != rightIndex) {
//基数由于在左边取,一定要右边开始
//右边索引停止条件
while (arr[rightIndex] >= baseNum && leftIndex < rightIndex) {
rightIndex--;
}
//左边索引停止条件
while (arr[leftIndex] <= baseNum && leftIndex < rightIndex) {
leftIndex++;
}
//左边和右边进行交换
int temp = arr[leftIndex];
arr[leftIndex] = arr[rightIndex];
arr[rightIndex] = temp;
}
//左、右索引重合时,左边和重合时中间的值交换
arr[left] = arr[leftIndex];
arr[leftIndex] = baseNum;
//递归左右两边
quickSort(arr, left, leftIndex - 1);
quickSort(arr, rightIndex + 1, right);
}
标签:arr,right,Java,int,索引,rightIndex,排序,快速,left 来源: https://www.cnblogs.com/youngyajun/p/14525969.html