Advertisement
Alisator

couting sort by column

Oct 18th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1.     public static int[][] sortArrayByColumn(int column) {
  2.         int[][] sortedArray = new int[M][3];
  3.         int min = hotels[M - 1][column];
  4.         int max = hotels[0][column];
  5.         //find min and max values for histogram from current column
  6.         for (int i = 1; i < M; i++) {
  7.             if (hotels[i][column] < min) {
  8.                 min = hotels[i][column];
  9.             } else if (hotels[i][column] > max) {
  10.                 max = hotels[i][column];
  11.             }
  12.         }
  13.         //histogram length from min to max
  14.         int[] hist = new int[max - min + 1];
  15.         for (int i = 0; i < M; i++) {
  16.             hist[hotels[i][column] - min]++;
  17.         }
  18.  
  19.         hist[0]--;
  20.         for (int i = 1; i < hist.length; i++) {
  21.             hist[i] = hist[i] + hist[i - 1];
  22.         }
  23.         //min to max
  24.         for (int i = M - 1; i >= 0; i--) {
  25.             int index = hist[hotels[i][column] - min];
  26.             sortedArray[index][column] = hotels[i][column];
  27.             sortedArray[index][1] = hotels[i][1];
  28.             sortedArray[index][0] = hotels[i][0];
  29.         }
  30.         return sortedArray;
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement