二维数组冒泡排序

13年前

一维数组冒泡排序:      int[] arr = new int[]{5,1,8,2,9,4,6,7,3,6,54};          int temp;  for(int i = 0; i < arr.length; i++){      for(int j = i + 1; j < arr.length; j++){           if(arr[i] > arr[j]){                temp = arr[i];                arr[i] = arr[j];                arr[j] = temp;            }       }  }    二维数组冒泡排序:  (1).转化为一维         int[][] arr2 = {{9,3,5},{2,7,4},{8,1,6}};         long s = System.nanoTime();         for(int i = 0; i < arr2.length; i++){             for(int j = 0 ; j < arr2[i].length; j++){                 for(int ii = i*arr2[i].length+j+1; ii < arr2.length*arr2[i].length; ii++){                     if(arr2[i][j] > arr2[ii/3][ii-(ii/3)*arr2[i].length]){                        temp = arr2[i][j];                        arr2[i][j] = arr2[ii/3][ii-(ii/3)*arr2[i].length];                        arr2[ii/3][ii-(ii/3)*arr2[i].length] = temp;                     }                 }             }         }         long e = System.nanoTime();         System.out.println("程序的运行时间是:"+(e-s)); //程序的运行时间是:6586  (2).直接二维排序          long s = System.nanoTime();          for(int i = 0; i < arr2.length; i++){             for(int j = 0 ; j < arr2[i].length; j++){                 for(int ii = 0; ii < arr2.length; ii++){                     for(int jj = 0; jj < arr2[ii].length; jj++){                         if(arr2[i][j] < arr2[ii][jj]){                             temp = arr2[ii][jj];                             arr2[ii][jj] = arr2[i][j];                             arr2[i][j] = temp;                         }                     }                                     }             }         }         long e = System.nanoTime();         System.out.println("程序的运行时间是:"+(e-s));//程序的运行时间是:7494  通过时间比较可得出:二维数组冒泡排序时转化为一维数组较快。