二维数组的选择排序算法是什么?我到处查看并阅读了有关二维数组的信息,但找不到任何简单直接的东西,所以我求助于论坛。(我似乎无法思考如何改变一维数组的类型以使其成为二维数组!!)
例如,这个:
name ----- crime ----- year
name1 ---- arson ----- 1996
name2 ---- theft ----- 2003
name3 ---- arson ----- 1976
name4 ---- theft ----- 2010
变成这样:
name ----- crime ----- year
name1 ---- arson ----- 1996
name3 ---- arson ----- 1976
name2 ---- theft ----- 2003
name4 ---- theft ----- 2010
任何帮助都会很棒!谢谢!
编辑
这是我用于选择排序的代码。他们中的大多数都组织得当,但在几行中,我有“纵火”不属于它,我不确定为什么会这样。这是我的代码:
for(i = 0; i < 10; i++){
smallest = i;
for(j = i; j < 10; j++){
if(criminals[i][1].compareTo(criminals[j][1]) > 0){
smallest = j;
}
}
temp = criminals[i][1];
criminals[i][1] = criminals[smallest][1];
criminals[smallest][1] = temp;
}
//output
for(i = 0; i < 10; i++){
System.out.println(criminals[i][0] + " - " + criminals[i][1] + " - " + criminals [i][2]);
}
这是我的输入和输出:
Not sorted list:
Al Capone - arson - 2009
Slippery Sal - theft - 2001
Nada - arson - 1987
Slippery Sal - theft - 1999
Salma - assault - 2010
Scooby Doo - theft - 1998
Velma - assault - 1991
Daphne - arson - 1976
Fred - assault - 2003
Shaggy - arson - 2007
Sorted list (by crime):
Al Capone - arson - 2009
Slippery Sal - arson - 2001
Nada - arson - 1987
Slippery Sal - assault - 1999
Salma - arson - 2010
Scooby Doo - assault - 1998
Velma - assault - 1991
Daphne - theft - 1976
Fred - theft - 2003
Shaggy - theft - 2007