fork download
  1. import java.util.Arrays;
  2.  
  3. class Ideone {
  4. private static final int MIN_COUNT = 3;
  5. public static void main(String[] args) {
  6. int[][] data = { { 74, 85, 123 },
  7. { 73, 84, 122 },
  8. { 72, 83, 121 },
  9. { 70, 81, 119 },
  10. { 69, 80, 118 },
  11. { 76, 87, 125 },
  12. { 77, 88, 126 },
  13. { 78, 89, 127 } };
  14. // Initialize min collectors
  15. Min[] min = new Min[data[0].length];
  16. for (int col = 0; col < min.length; col++)
  17. min[col] = new Min(MIN_COUNT);
  18. // Collect data
  19. for (int row = 0; row < data.length; row++)
  20. for (int col = 0; col < min.length; col++)
  21. min[col].add(data[row][col]);
  22. // Print result
  23. for (int i = 0; i < MIN_COUNT; i++) {
  24. for (int col = 0; col < min.length; col++)
  25. System.out.printf("min%d = %-5d ", i + 1, min[col].get(i));
  26. System.out.println();
  27. }
  28. }
  29. }
  30. class Min {
  31. private int[] min;
  32. public Min(int count) {
  33. this.min = new int[count];
  34. Arrays.fill(this.min, Integer.MAX_VALUE);
  35. }
  36. public void add(int value) {
  37. int idx = Arrays.binarySearch(this.min, value);
  38. if (idx != -this.min.length - 1) { // not insert at end
  39. if (idx < 0)
  40. idx = -idx - 1;
  41. System.arraycopy(this.min, idx, this.min, idx + 1, this.min.length - idx - 1);
  42. this.min[idx] = value;
  43. }
  44. }
  45. public int get(int index) {
  46. return this.min[index];
  47. }
  48. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
min1 = 69    min1 = 80    min1 = 118   
min2 = 70    min2 = 81    min2 = 119   
min3 = 72    min3 = 83    min3 = 121