请考虑使用快速排序
例如:
public class QuickSort {
private static int[] input = { 3, 5, 3, 76, 45, 23, 7, 44, 25, 54 };
private static void exchange(int from, int to) {
int temp = input[from];
input[from] = input[to];
input[to] = temp;
}
private static int partition(int p, int r) {
int x = input[r];
int i = p - 1;
for (int j = p; j < r; j++) {
if (input[j] <= x) {
i++;
exchange(i, j);
}
}
exchange(i + 1, r);
return i + 1;
}
private static void quickSort(int p, int r) {
if (p < r) {
int q = partition(p, r);
quickSort(p, q - 1);
quickSort(q + 1, r);
}
}
private static void printResult() {
for (int i = 0; i < input.length; i++) {
System.out.println("Index : " + i + "; Value = " + input[i]);
}
}
// nlgn
public static void main(String[] args) {
quickSort(0, input.length - 1);
printResult();
}
}
温馨提示:内容为网友见解,仅供参考