我有两个带字符串的数组1和另一个带有整数的数组。 我必须使用插入排序以数字顺序打印此列表这是我的代码到目前为止
这些是数组:
String[]bn={"Cardinals","BlueJays","Albatross","Vultures","Crows","Mockingbirds","Condors","BaldEagles","Pigeons","RedHeadWoodPecker","Hummingbirds","Dodos"};
int[]bq={40,15,1,3,10,2,12,25,7,6,88,15};
public static void SortNumericalOrdernsert (String[] bn,int[] bq){
for(int i=1;i<bq.length;i++){
int next=bq[i];
String y=bn[i];
//find all the insertion location
//Move all the larger elements up
int j=i;
while(j>0 && bq[j-1]>next){
bn[j]=bn[j-1];
bq[j]=bq[j-1];
j--;
}
//insert the element
bq[j]=next;
bn[j]=y;
}
}}
我在哪里做错了?
答案 0 :(得分:0)
//编辑
你想这样做吗?
public static void SortNumericalOrdernsert(String[] bn, int[] bq) {
for (int i = 1; i < bq.length; i++) {
int next = bq[i];
// find all the insertion location
// Move all the larger elements up
int j = i;
while (j > 0 && bq[j - 1] > next) {
bq[j] = bq[j - 1];
j--;
}
bq[j] = next;
}
for (int i = 1; i < bn.length; i++) {
String y = bn[i];
int j = i;
while (j > 0 && isBigger(bn[j - 1], y)) {
bn[j] = bn[j - 1];
j--;
}
bn[j] = y;
}
}
private static boolean isBigger(String left, String right) {
return left.compareTo(right) > 0;
}