/**
* Created by L on 2017/3/18.
*/
public class Main {
public static void main(String[] args) {
int[] arr = {54, 34, 365, 3, 1, 56, 8, 5, 4356, 43242,4356,467, 98, 45, 97, 67, 43, 342, 0, 345,1,0};
System.out.println("排序前长度:" + arr.length);
sort(arr, 0, arr.length - 1);
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println("\n排序后长度:" + arr.length);
}
private static void sort(int[] arr, int low, int high) {
if (low < high) {
int mid = partition(arr, low, high);
sort(arr, low, mid - 1);
sort(arr, mid + 1, high);
}
}
private static int partition(int[] arr, int low, int high) {
int key = arr[low];
while (low < high) {
while (low < high && arr[high] >= key) --high;
arr[low] = arr[high];
while (low < high && arr[low] <= key) ++low;
arr[high] = arr[low];
}
arr[low] = key;
return low;
}
}
Java快速排序
最新推荐文章于 2023-06-04 10:54:50 发布