快速排序
时间复杂度O(nlogn)
空间复杂度O(nlogn)
import java.util.Arrays; public static void quickSort(int[] arr,int left, int right) { if(left>right){ return; } int base = arr[left]; int i = left; //左下标 int j = right; //右下标 while(i != j) { while( arr[j] >= base && i < j) { j--; } while(arr[i] <= base && i < j) { i++; } if(i < j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } arr[left] = arr[i]; arr[i] = base; quickSort(arr, left, i-1); quickSort(arr, i+1, right); } public static void main(String[] args){ int[] a = { 49, 38, 65, 97, 76, 13, 27, 50 }; quickSort(a, 0, a.length-1); System.out.println("排好序的数组:"); for (int e : a) System.out.print(e+" "); } }