// 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();
}
}
}
'Algorithm > Problem Solving' 카테고리의 다른 글
알고리즘 과제 저장1 (0) | 2021.11.10 |
---|---|
[Java] 삽입 정렬(insertion sort) (0) | 2021.09.11 |
[Python] SWEA 2019번 : 더블더블 (0) | 2021.08.18 |
[Python] SWEA 2050번 : 알파벳을 숫자로 변환 (0) | 2021.08.18 |
[Python] SWEA 2063번 : 중간값 찾기 (0) | 2021.08.18 |
댓글