public class Sort {
public static void rSort(int[] a, int p, int r) {
if (p >= r) {
System.out.println(" p= " + p + " r = " + r);
return;
}
int q = partition(a, p, r);
if (q > 0) {
for (int i = 0; i <= q - 1; i++) {
System.out.print(a[i] + " ");
}
}
System.out.print("[" + a[q] + "] ");
for (int i = q + 1; i < a.length; i++) {
System.out.print(a[i] + " ");
}
rSort(a, p, q - 1);
rSort(a, q + 1, r);
}
public static int partition(int[] a, int p, int r) {
if (r > a.length - 1 || r < p) {
return p;
}
int i = p - 1;
int j = p;
int x = a[r];
while (j <= r - 1) {
if (a[j] < x) {
i++;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
j++;
}
int temp = a[r];
a[r] = a[i + 1];
a[i + 1] = temp;
System.out
.println(" p= " + p + " r = " + r + " partition = " + (i + 1));
return i + 1;
}
public static void main(String[] args) {
int[] arr = { 5, 7, 9, 3, 16, 8, 14, 47, 31 };
rSort(arr, 0, arr.length - 1);
}
}
07-18
07-18
07-18
07-18
07-18
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交