Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int[][] sortArrayByColumn(int column) {
- int[][] sortedArray = new int[M][3];
- int min = hotels[M - 1][column];
- int max = hotels[0][column];
- //find min and max values for histogram from current column
- for (int i = 1; i < M; i++) {
- if (hotels[i][column] < min) {
- min = hotels[i][column];
- } else if (hotels[i][column] > max) {
- max = hotels[i][column];
- }
- }
- //histogram length from min to max
- int[] hist = new int[max - min + 1];
- for (int i = 0; i < M; i++) {
- hist[hotels[i][column] - min]++;
- }
- hist[0]--;
- for (int i = 1; i < hist.length; i++) {
- hist[i] = hist[i] + hist[i - 1];
- }
- //min to max
- for (int i = M - 1; i >= 0; i--) {
- int index = hist[hotels[i][column] - min];
- sortedArray[index][column] = hotels[i][column];
- sortedArray[index][1] = hotels[i][1];
- sortedArray[index][0] = hotels[i][0];
- }
- return sortedArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement