62,620
社区成员
发帖
与我相关
我的任务
分享
public class ArraysSort {
public static void main(String[] args) {
int[][] a = new int[2][];
a[0] = new int[]{1,3,3,5,7,4,2,6,8};
a[1] = new int[a[0].length];
for(int i=0; i<a[1].length; i++) {
a[1][i] = i;
}
sort(a);
for(int i=0; i<a[1].length; i++) {
System.out.print(a[1][i] + "\t");
}
}
public static void sort(int[][] a) {
for(int i=0; i<a[0].length-1; i++) {
int changeIndex = i;
for(int j=i+1; j<a[0].length; j++) {
if(a[0][changeIndex] > a[0][j]) {
changeIndex = j;
}
}
if(i != changeIndex) {
a[0][i] ^= a[0][changeIndex];
a[0][changeIndex] ^= a[0][i];
a[0][i] ^= a[0][changeIndex];
a[1][i] ^= a[1][changeIndex];
a[1][changeIndex] ^= a[1][i];
a[1][i] ^= a[1][changeIndex];
}
}
}
}
/*
0 6 2 1 5 3 7 4 8
*/
int a[]={1,3,3,5,7,4,2,6,8};
TreeSet ts=new TreeSet();
for(int i=0;i<a.length;i++){
ts.add(a[i]);
}
Iterator itr=ts.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}