Home > DeveloperSection > Blogs > Two Dimensional array with its sorting using java

Two Dimensional array with its sorting using java


Java Java 
Ratings:
0 Comment(s)
 2018  View(s)
Rate this:

Two Dimensional array with its sorting using java

In this blog I am trying to make a two dimensional array accepted by user and the find the maximum and minimum value with its index and also show the matrix with sorted (ascending) order.

import java.io.*;

import java.util.Arrays;

import java.util.Comparator;

public class matrix2D

{

    static int indexRow=0;

    static int indexColumn=0;

    public static void main(String[] args){

    

                System.out.print("Enter the number of Rows : ");

                int row= Integer.parseInt(System.console().readLine());

                System.out.println();

                System.out.print("Enter the number of Column : ");

                int col= Integer.parseInt(System.console().readLine());

                int[][] matrix2D = new int[row][col]; 

                System.out.println("Enter the values of Matrix");

                System.out.println();

                for(int i=0;i<row;i++)

                {

                                for(int j=0;j<col;j++)

                                {

                                               System.out.print("["+i+"]["+j+"] : ");

                                                matrix2D[i][j]= Integer.parseInt(System.console().readLine());

                                }             

                }

                System.out.println("::Matrix::");

                System.out.println();    

                for(int i=0;i<row;i++)

                {

                                for(int j=0;j<col;j++)

                                {

                                                System.out.print(matrix2D[i][j]+"\t");

                                }

                                System.out.println("\n");                           

                }

                System.out.println();

                System.out.println("::Maximum and Minimum Values in Matrix::\n");

                System.out.println();

                int minimumValue=min(matrix2D);

                System.out.println("Minimum Value is "+minimumValue+" at ["+indexRow+"]["+indexColumn+"] Position");

                int maximumValue=max(matrix2D);

                System.out.println("Maximum Value is "+maximumValue+" at ["+indexRow+"]["+indexColumn+"] Position");

                sortMatrix(matrix2D);

    }

    public static int min(int[][] matrix)

    {

        int min = matrix[0][0];

        for (int col = 0; col < matrix.length; col++)

        {

            for (int row = 0; row < matrix[col].length; row++)

                    {

                if (min > matrix[col][row])

                                {

                   min = matrix[col][row];

                                   indexRow=col;

                   indexColumn=row;

                }

            }

        }

        return min;

    }

    public static int max(int[][] matrix)

    {

        int max = matrix[0][0];

        for (int col = 0; col < matrix.length; col++) {

            for (int row = 0; row < matrix[col].length; row++) {

                if (max < matrix[col][row]) {

                    max = matrix[col][row];

                                    indexRow=col;

                    indexColumn=row;

                }

            }

        }

        return max;

    }

    public static void sortMatrix(int[][] array)

    {

                for (int row = 0; row < array.length; row++)

                {

                for (int col = 0; col < array[0].length; col++)

                                {

                                for (int colTwo = col + 1; colTwo < array[0].length; colTwo++)

                                                {

                                                if (array[row][colTwo] < array[row][col])

                                                swap(array, row, col, colTwo);

                                }

                }

        }

                System.out.println();

                System.out.println("::Sorted Matrix::");

                System.out.println();                    

                for (int r = 0; r < array.length; r++)

                {

                for (int c = 0; c < array[0].length; c++)

                                {

                                System.out.print(array[r][c] + "\t");

                }

                                System.out.println("\n");            

                }

    }

    public static void swap(int[][] array, int r, int c, int colTwo)

    {

                int temp = array[r][c];

                        array[r][c] = array[r][colTwo];

                array[r][colTwo] = temp;

    }  

}                           


Output: -


Don't want to miss updates? Please click the below button!

Follow MindStick