public class test {
public static void main(String[] args) {
int N = 10;
int[] a = new int[N];
for (int i = 0; i < N; i++) {
a[i] = (int) (Math.random() * 100);
System.out.print(a[i] + "\t");
}
System.out.println();
sort(a, 0, N - 1);
for (int i = 0; i < N; i++) {
System.out.print(a[i] + "\t");
}
}
private static void sort(int[] a, int left, int right) {
int l = left;
int r = right;
// 以a[left]为基准
while (l < r) {
while (l < r) {
if (a[l] > a[r]) {
swap(a, l, r);
break;
}
r--;
}
while (l < r) {
if (a[l] > a[r]) {
swap(a, l, r);
break;
}
l++;
}
}
if (left < l - 1) {
sort(a, left, l - 1);
}
if (r + 1 < right) {
sort(a, r + 1, right);
}
}
private static void swap(int[] a, int m, int n) {
int t = a[m];
a[m] = a[n];
a[n] = t;
}
}