java中二维数组的算法,急! 现有如下二维数组 {{2,9},{2,8},{1,7}{3,6}}进行排序 结果要求是:{{1,7},{2,8},{2,9},{3,6}} 求源代码。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 随便写了下,不难的,给lz参考一下,复杂度n2,你可以自己写个排序效率高点的算法public class Array2Comp { public static void main(String[] args) { int[][] iAry = {{2,9},{2,8},{1,7},{3,6}}; compare(iAry); for (int i = 0; i < iAry.length; i++) { for (int j = 0; j < 2; j++) { System.out.print(iAry[i][j] + " "); } System.out.println(); } } public static void compare(int[][] ary) { int row = ary.length; for (int i = 0; i < row - 1; i++) { for (int j = i + 1; j < row; j++) { if (ary[i][0] > ary[j][0]) { int tempR = ary[i][0]; int tempC = ary[i][1]; ary[i][0] = ary[j][0]; ary[i][1] = ary[j][1]; ary[j][0] = tempR; ary[j][1] = tempC; } else if (ary[i][0] == ary[j][0]) { if (ary[i][1] > ary[j][1]) { int tempR = ary[i][0]; int tempC = ary[i][1]; ary[i][0] = ary[j][0]; ary[i][1] = ary[j][1]; ary[j][0] = tempR; ary[j][1] = tempC; } } } } }} 写了一个局限性非常大的quick sort 只能解这题,仅供参考。public class twoDArraySort{ public static void main(String [] args) { int [][] s = {{2,9},{2,8},{1,7},{3,6}}; for(int i = 0;i<4;i++) { for(int j = 0;j<2;j++) { System.out.print(s[i][j]+" "); } System.out.println(); } quickSort(s,0,3); System.out.println("After quick sort."); for(int i = 0;i<4;i++) { for(int j = 0;j<2;j++) { System.out.print(s[i][j]+" "); } System.out.println(); } } public static void swap(int [][] s,int i,int j) { int [][] temp = new int[1][2]; temp[0][0] = s[i][0]; temp[0][1] = s[i][1]; s[i][0] = s[j][0]; s[i][1] = s[j][1]; s[j][0] = temp[0][0]; s[j][1] = temp[0][1]; } public static void quickSort(int [][] s,int begin,int end) { if(begin<end) { int pivot = partition(s,begin,end); quickSort(s,begin,pivot-1); quickSort(s,pivot+1,end); } } public static int partition(int [][] s,int begin,int end) { int mid = (begin + end)/2; int pivotX = s[mid][0]; int pivotY = s[mid][1]; swap(s,mid,end); while(begin<end) { for(;begin<end&&s[begin][0]<pivotX;begin++) ; if(begin<end) { swap(s,begin,end); end--; } for(;begin<end&& (s[end][0]>pivotX || (s[end][0]==pivotX && s[end][1]<pivotY)); end--) ; if(begin<end) { swap(s,begin,end); end++; } } return end; } } output:2 92 81 73 6After quick sort.1 72 82 93 6 求完成的POI读取和生成execl的例子 Struts2的action中的成员变量 select标签 关于java读写txt文件的疑问? 请教 iReport 问题 【50分求思路】:JSP动态表单中多条记录一起提交-->Servlet中怎么接? httpclient抓取网页的问题 求助 java就是那么回事,炒作太多了 java导入数据库时剔除掉手机尾数4、7的不让导入进去 问个url中查询参数的问题 定义XML Schema使用哪种定义方式比较好?
public class Array2Comp {
public static void main(String[] args) {
int[][] iAry = {{2,9},{2,8},{1,7},{3,6}};
compare(iAry);
for (int i = 0; i < iAry.length; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(iAry[i][j] + " ");
}
System.out.println();
}
}
public static void compare(int[][] ary) {
int row = ary.length;
for (int i = 0; i < row - 1; i++) {
for (int j = i + 1; j < row; j++) {
if (ary[i][0] > ary[j][0]) {
int tempR = ary[i][0];
int tempC = ary[i][1];
ary[i][0] = ary[j][0];
ary[i][1] = ary[j][1];
ary[j][0] = tempR;
ary[j][1] = tempC;
} else if (ary[i][0] == ary[j][0]) {
if (ary[i][1] > ary[j][1]) {
int tempR = ary[i][0];
int tempC = ary[i][1];
ary[i][0] = ary[j][0];
ary[i][1] = ary[j][1];
ary[j][0] = tempR;
ary[j][1] = tempC;
}
}
}
}
}
}
public class twoDArraySort
{
public static void main(String [] args)
{
int [][] s = {{2,9},{2,8},{1,7},{3,6}};
for(int i = 0;i<4;i++)
{
for(int j = 0;j<2;j++)
{
System.out.print(s[i][j]+" ");
}
System.out.println();
}
quickSort(s,0,3);
System.out.println("After quick sort.");
for(int i = 0;i<4;i++)
{
for(int j = 0;j<2;j++)
{
System.out.print(s[i][j]+" ");
}
System.out.println();
}
}
public static void swap(int [][] s,int i,int j)
{
int [][] temp = new int[1][2];
temp[0][0] = s[i][0];
temp[0][1] = s[i][1];
s[i][0] = s[j][0];
s[i][1] = s[j][1];
s[j][0] = temp[0][0];
s[j][1] = temp[0][1];
}
public static void quickSort(int [][] s,int begin,int end)
{
if(begin<end)
{
int pivot = partition(s,begin,end);
quickSort(s,begin,pivot-1);
quickSort(s,pivot+1,end);
}
}
public static int partition(int [][] s,int begin,int end)
{
int mid = (begin + end)/2;
int pivotX = s[mid][0];
int pivotY = s[mid][1];
swap(s,mid,end);
while(begin<end)
{
for(;begin<end&&s[begin][0]<pivotX;begin++)
;
if(begin<end)
{
swap(s,begin,end);
end--;
}
for(;begin<end&&
(s[end][0]>pivotX || (s[end][0]==pivotX &&
s[end][1]<pivotY));
end--)
;
if(begin<end)
{
swap(s,begin,end);
end++;
}
} return end;
}
}
2 8
1 7
3 6
After quick sort.
1 7
2 8
2 9
3 6