public class Test { public static void main(String[] args) {
// 需要排列的数组a
int[] a = new int[] { 3, 22, 11, 5, 400, 99, 20, 22, 5 };
// 需要记录下标的数组b
int[] index = new int[a.length];
// 初始化下标
for (int i = 0; i < index.length; i++) {
index[i] = i;
}
// 用冒泡法排序
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
// 下标排序
int tempindex = index[i];
index[i] = index[j];
index[j] = tempindex;
}
}
}
// 打印输出
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println("");
for (int i = 0; i < a.length; i++) {
System.out.print(index[i] + " ");
}
}
}
输出结果:
3 5 5 11 20 22 22 99 400
0 3 8 2 6 7 1 5 4