快速排序为什么快?
快速排序的“快”是因为使用了“分治法”,使其时间复杂度降低到了(nlogn)
实现的方法
- 递归 (双边循环法 / 单边循环法)
- 非递归
代码实现
双边循环
/**
* @Author qucg
* @Version 2019/5/19
* @Description 快速排序,快速排序的“快”是因为使用了“分治法”
*/
public class QuickSort {
public static void quickSort(int[] arr, int startIndex, int endIndex) {
// 递归结束条件
if (startIndex >= endIndex) {
return;
}
// 得到基准元素位置
int pivotIndex = partition(arr, startIndex, endIndex);
// 根据基准元素,分成两部分进行递归排序
quickSort(arr, startIndex, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, endIndex);
}
/**
* 分治(双边循环法)
*
* @param arr 待交换的数组
* @param startIndex 起始下标
* @param endIndex 结束下标
* @return 重叠的索引
*/
private static int partition(int[] arr, int startIndex, int endIndex) {
// 取第1个位置(也可以选择随机位置)的元素作为基准
int pivot = arr[startIndex];
int left = startIndex;
int right = endIndex;
// 循环移动左右指针
while (left != right) {
// right左移
while (left < right && arr[right] > pivot) {
right--;
}
// left右移
while (left < right && arr[left] <= pivot) {
left++;
}
// 交换left和right指针所指向的元素
if (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
}
// pivot和指针重合点交换
arr[startIndex] = arr[left];
arr[left] = pivot;
return left;
}
public static void main(String[] args) {
int[] arr = new int[]{4,4,6,5,3,2,8,1};
quickSort(arr, 0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
单边循环法
/**
* @author qcg
* @version 2019/5/21.
* @description 单边快速排序实现
* 确定两个元素:
* 1.pivot基准
* 2.mark指针指向代表小于基准元素的边界
* 基本思路:
* 1.每个数组元素和基准值去比较
* 2.如果数组元素小于基准元素:
* 2.1 mark++
* 2.2 将mark指向的元素与该数组元素交换
* 3.以mark指向的元素为准,左右数组分别重复第一步
*/
public class SingleSideQuickSort {
public static void quickSort(int[] arr, int startIndex, int endIndex) {
// 递归结束条件
if (startIndex >= endIndex) {
return;
}
// 得到基准元素位置
int pivotIndex = partition(arr, startIndex, endIndex);
// 根据基准元素,分成两部分进行递归排序
quickSort(arr, startIndex, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, endIndex);
}
/**
* 分治(单边循环法)
*
* @param arr 待交换的数组
* @param startIndex 起始下标
* @param endIndex 结束下标
* @return 小于基准元素的边界
*/
private static int partition(int[] arr, int startIndex, int endIndex) {
int pivot = arr[startIndex];
int mark = startIndex;
for (int i = startIndex + 1; i <= endIndex; i++) {
if (arr[i] < pivot) {
mark++;
int temp = arr[mark];
arr[mark] = arr[i];
arr[i] = temp;
}
}
// mark指向的元素和pivot交换
arr[startIndex] = arr[mark];
arr[mark] = pivot;
return mark;
}
// 第一轮排序:[1, 3, 2, 4, 4, 6, 8, 5]
public static void main(String[] args) {
int[] arr = new int[]{4, 4, 6, 5, 3, 2, 8, 1};
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
}
非递归实现
/**
* @Author qucg
* @Version 2019/5/21
* @Description 非递归实现的快速排序
*/
public class NonRecursiveQuickSort {
/**
* 快排
* @param arr
* @param startIndex
* @param endIndex
*/
public static void quickSort(int[] arr, int startIndex, int endIndex) {
// 用一个集合栈来代替递归的函数栈
Stack<Map<String, Integer>> quickSortStack = new Stack<Map<String, Integer>>();
// 整个数列的起止下标,以哈希的形式入栈
Map rootParam = new HashMap();
rootParam.put("startIndex", startIndex);
rootParam.put("endIndex", endIndex);
quickSortStack.push(rootParam);
// 循环结束条件:栈为空时
while (!quickSortStack.isEmpty()) {
// 栈顶元素出栈,得到起止下标
Map<String, Integer> param = quickSortStack.pop();
// 得到基准元素位置
int pivotIndex = partition(arr, param.get("startIndex"), param.get("endIndex"));
// 根据基准元素分为两部分,把每一部分的起止下标入栈
if (param.get("startIndex") < pivotIndex - 1) {
Map<String, Integer> leftParam = new HashMap<String, Integer>();
leftParam.put("startIndex", param.get("startIndex"));
leftParam.put("endIndex", pivotIndex - 1);
quickSortStack.push(leftParam);
}
if (pivotIndex + 1 < param.get("endIndex")) {
Map<String, Integer> rightParam = new HashMap<String, Integer>();
rightParam.put("startIndex", pivotIndex + 1);
rightParam.put("endIndex", param.get("endIndex"));
quickSortStack.push(rightParam);
}
}
}
/**
* 分治 (单边循环法)
*
* @param arr 待交换的数组
* @param startIndex 起始下标
* @param endIndex 结束下标
* @return 边界
*/
private static int partition(int[] arr, int startIndex, int endIndex) {
int pivot = arr[startIndex];
int mark = startIndex;
for (int i = startIndex + 1; i <= endIndex; i++) {
if (arr[i] < pivot) {
mark++;
int temp = arr[mark];
arr[mark] = arr[i];
arr[i] = temp;
}
}
// 交换mark指向的元素与pivot
arr[startIndex] = arr[mark];
arr[mark] = pivot;
return mark;
}
public static void main(String[] args) {
int[] arr = new int[]{4,7,6,5,3,2,8,1};
quickSort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
因为其快的特点,常常用于大规模的数据排序场景