关于快速排序的相关内容
快速排序的思路:
https://lixj.fun/upload/2021/07/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F-eadb58e4707d48d6a047cecc1b637849.gif
算法复杂度:O(nlogn)
算法空间复杂度:O(1)
算法稳定性:不稳定
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
int i,j,temp;
if (i > j) {
return;
}
i = low;
j = high;
temp = arr[low];
while (i < j) {
while (arr[j] >= temp && i < j) {
j--;
}
while (arr[i] <= temp && i < j) {
i++;
}
if (i < j) {
// 交换i和j的值
swap(arr, i, j);
}
}
// 交换low 和 i 的值
swap(arr, low, i);
quickSort(arr, low, i-1);
quickSort(arr, i+1, high);
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args){
int[] arr = {10,7,2,4,7,62,3,4,2,1,8,9,19};
// int[] arr = {6 ,1 ,2, 7 ,9 ,3 ,4 ,5 ,10 ,8};
quickSort(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links: https://lixj.fun/archives/快速排序