一.代码
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);}private static int partition(int[] arr, int startIndex, int endIndex) {int pivot = arr[startIndex];int left = startIndex;int right = endIndex;while (left != right) {while (left < right && arr[right] >= pivot) {right--;}while (left < right && arr[left] <= pivot) {left++;}if (left < right) {int temp = arr[left];arr[left] = arr[right];arr[right] = temp;}}arr[startIndex] = arr[left];arr[left] = pivot;return left;}}
二.稳定性
不稳定。5 3 4 3 6 7 10 排序 3 3 4 5 6 7 10 ,3的顺序变了。
三.时间复杂度
最优O(nlogn) 最差O(n^2) 平均O(nlogn)
