알고리즘 과제 저장2

    // PA2
    
    public class Matrix {
        private int[][] element;
    
        public Matrix(){
            element = null;
        }
    
        public int[][] getMatrix(){
            return element;
        }
    
        public void clearMatrix(){
            int i=0, m;
            if(element == null) return;
            m = element.length;
            for(i=0; i<m; i++)  element[i] =null;
            element = null;
        }
    
        public void insertElements(int[][] _e){
            int row = _e.length;
            int col = _e[0].length;
            element = new int [row][col];
    
            //element = _e;
            for(int i=0; i<row; i++){
                for(int j=0; j<col; j++){
                    element[i][j] = _e[i][j];
                }
            }
            element = getTransposed(element);
            sortRow(element);
            element = getTransposed(element);
            sortRow(element);
        }
    
        public void sortRow(int matrix[][]){
            int row = matrix.length;
            int col = matrix[0].length;
            for (int i = 0; i < row; i++)
                 // sorting row number 'i'
                quickSort(matrix[i], 0, col-1 );
        }
        
        public int[][] getTransposed(int[][] matrix){
           
            int row = matrix.length;
            int col = matrix[0].length;
           
            int[][] transposedMatrix = new int[col][row];
            for(int x = 0; x < col; x++) {
                for(int y = 0; y < row; y++) {
                    transposedMatrix[x][y] = matrix[y][x];
                }
            }
            return transposedMatrix;
        }
        
        private void swap(int arr[], int index1, int index2) {
           int temp = arr[index1];
           arr[index1] = arr[index2];
           arr[index2] = temp;
        }
        
        private void quickSort(int[] a, int lo, int hi) {
           if(lo >= hi) {
              return;
           }
           int pivot = partition(a, lo, hi);   
           quickSort(a, lo, pivot - 1);
           quickSort(a, pivot + 1, hi);
        }
        
        private int partition(int[] a, int left, int right) {
           
           int lo = left;
           int hi = right;
           int pivot = a[left];   
           while(lo < hi) {
              while(a[hi] > pivot && lo < hi) {
                 hi--;
              }
              while(a[lo] <= pivot && lo < hi) {
                 lo++;
              }
              swap(a, lo, hi);
           }
           swap(a, left, lo);
           return lo;
        }
    
        public static void main(String args[]) throws Exception{
            int i=0, j=0;
            int[][] elements = {{ 9,16,3,0x7FFFFFFF}, { 4, 2, 0x7FFFFFFF, 0x7FFFFFFF}, { 8, 5, 0x7FFFFFFF, 0x7FFFFFFF}, { 0x7FFFFFFF, 14, 0x7FFFFFFF, 12}};
            int[][] r;
    
            Matrix mat = new Matrix();
            mat.insertElements(elements);
            r = mat.getMatrix();
    
            for(i=0;i<r.length;i++){
                for(j=0;j<r[0].length;j++){
                    if(r[i][j] == 0x7FFFFFFF)   System.out.print("Max\t");
                    else                        System.out.print(r[i][j] + "\t");
                }
                System.out.println();
            }
        }
    }

    댓글