網路流 一定在最大匹配裡的邊
阿新 • • 發佈:2020-11-17
快速排序使用分治法(Divide and conquer)策略來把一個序列(list)分為兩個子序列(sub-lists)。
快速排序又是一種分而治之思想在排序演算法上的典型應用。本質上來看,快速排序應該算是在氣泡排序基礎上的遞迴分治法。
public class Test { public static void main(String[] args) { int[] arr = {49, 38, 65, 97, 23, 76, 1}; quickSort(arr, 0, arr.length - 1); System.out.println("排序後:"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } public static void quickSort(int[] arr, int low, int high) { if (low >= high) { return; } int index = getIndex(arr, low, high); quickSort(arr, low, index- 1); quickSort(arr, index + 1, high); } public static int getIndex(int[] arr, int low, int high) { int temp = arr[low]; while (low < high) { while (low < high && arr[high] >= temp) { high--; } arr[low]= arr[high]; while (low < high && arr[low] < temp) { low++; } arr[high] = arr[low]; } arr[low] = temp; return low; } }