我的任务要求我将输入的数据存储在二维数组中,并使用选择排序按字母顺序对犯罪进行排序。如何更改以下选择排序:
//selection sort
for(i = 0; i < criminal.length; i++){
smallest = i;
for(j = i; j < criminal.length; j++){
//compare smallest to current position
if(criminal[j] < criminal[smallest]){
smallest = j;
}
//swap smallest with position in the array
temp = criminal[i];
criminal[i] = criminal[smallest];
age[smallest] = temp;
}
}
//output
for(i = 0; i < criminal.length; i++){
System.out.println(criminal[i]);
}
这样它就可以容纳如下所示的二维数组:
//loop to request to fill array
for (i = 0; i < criminals.length ; i++) {
System.out.print("Enter first and last name of criminal: ");
criminals[i][0] = br.readLine(); //Criminal Name
System.out.print("Enter crime committed: ");
criminals[i][1] = br.readLine(); //Criminal Crime
System.out.print("Enter year of conviction: ");
criminals[i][2] = br.readLine(); //Year of conviction
}
而已。任何帮助都会很棒!我真的需要尽快完成这件事。提前致谢。:)