现有如下二维数组 {{2,9},{2,8},{1,7}{3,6}}进行排序 结果要求是:{{1,7},{2,8},{2,9},{3,6}} 求源代码。谢谢

解决方案 »

  1.   

    随便写了下,不难的,给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;
    }
    }
    }
    }
    }
    }
      

  2.   

    写了一个局限性非常大的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;
          }
       }
      

  3.   

    output:2 9
    2 8
    1 7
    3 6
    After quick sort.
    1 7
    2 8
    2 9
    3 6