import java.util.Arrays;
public class QuickSort {
public static void sort(int[] arr, int head, int tail) {
if (head >= tail || arr == null || arr.length <= 1) {
return;
}
int i = head, j = tail, pivot = arr[(head + tail) / 2];
while (i <= j) {
while (arr[i] < pivot) {
++i;
}
while (arr[j] > pivot) {
--j;
}
if (i < j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
++i;
--j;
} else if (i == j) {
++i;
}
}
sort(arr, head, j);
sort(arr, i, tail);
}
public static void main(String[] args) {
int[] ints = {5, 3, 4, 1, 2};
sort(ints, 0, ints.length - 1);
System.out.println(Arrays.toString(ints));
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-878.html