public class SortUtil {
public static void sort(int[] a) {
sort(a, 0, a.length);
}
private static void sort(int[] a, int i, int j) {
if (a == null || j == 1) {
return;
}
if (a[i] < a[i + 1]) {
int m = a[i];
a[i] = a[i + 1];
a[i + 1] = m;
}
if (i < j - 2) {
sort(a, i + 1, j);
} else {
sort(a, 0, j - 1);
}
}
}
public class SortTest {
public static void main(String[] args) {
int[] a = new int[] { 2, 1, 3, 5, 4, 7, 6, 8 };
SortUtil.sort(a);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < a.length; i++) {
if (i < a.length - 1) {
sb.append(a[i] + ", ");
} else {
sb.append(a[i]);
}
}
System.out.println(sb);
}
}
转载于:https://blog.51cto.com/9277822/1543635