Advertisement
Shavit

P. 135 Ex. 12.18

Mar 23rd, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. public class PartiallySortedMatrix
  5. {
  6.     static int[][] matrix;
  7.    
  8.     public PartiallySortedMatrix(int[][] matrix)
  9.     {
  10.         this.matrix = matrix.clone();
  11.     }
  12.    
  13.     public static void searchValue(int value)
  14.     {
  15.         int col;
  16.         for(int i = 0; i < matrix.length; i++)
  17.         {
  18.             col = findKey(matrix, i, value);
  19.             if(col != -1)
  20.                 System.out.printf("%d was found in matrix[%d][%d]", value, i, col);
  21.         }
  22.     }
  23.    
  24.     public static int findKey(int[][] array, int row, int key)
  25.     {
  26.         for(int i = 0; i < array[row].length; i++)
  27.         {
  28.             if(array[row][i] > key)
  29.                 break;
  30.             else if(array[row][i] == key)
  31.                 return i;
  32.         }
  33.         return -1;
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement