Open In App

Java Program to Sort 2D Array Across Left Diagonal

Last Updated : 10 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implements the List interface, so we can use all the methods of List interface here. This program is used to Sort the 2D array Across Left Diagonal. We will use the concept of vector to sort Left Diagonal prior to which refer to below illustration as follows.

Illustration: Creating a Vector 

Vector(): Creates a default vector of the initial capacity is 10.

Vector<E> v = new Vector<E>();

Algorithm:

  • Traverse Diagonal element one by one. Condition is  (i==j) or ( arr[i][i])
  • Add elements of Diagonal in vector v.
  • Sort the vector.
  • Push back the sorted elements from vector to Diagonal.

Now let us discuss functions that will be used in the above approach prior to landing upon the implementation part.

A. removeAll(): The java.util.vector.removeAll(Collection col) method is used to remove all the elements from the vector, present in the collection specified.

Syntax:

Vector.removeAll(Vector);  

B. Collections.sort(): This method is used to sort the vector.

Syntax:

Collections.sort(Vector) ;

C. add(): It to add elements in the vector.

Syntax:

Vector.add(value) ;

D. get(): This method will get an element of Vector stored at a particular index.

Syntax:

Vector.get(3);

Example:

Java




// Java Program to Sort 2D Array Across Left Diagonal
 
// Importing required classes
import java.io.*;
import java.lang.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws java.lang.Exception
    {
 
        // Custom input 2D array
        int[][] arr
            = { { 5, 2, 0, 7, 1 }, { 3, 4, 2, 9, 14 },
                { 5, 1, 3, 5, 2 }, { 4, 2, 6, 2, 1 },
                { 0, 6, 3, 5, 1 }, { 1, 4, 7, 2, 8 } };
 
        // Display message
        System.out.println("Matrix without sorting \n");
 
        // Nested for loops to display 2D matrix
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
 
                // Printing elements
                System.out.print(arr[i][j] + " ");
            }
 
            // By now we are done with one row so
            // new line is needed
            System.out.println();
        }
 
        // Creating an object of Vector class
        Vector<Integer> v = new Vector<>();
 
        // Adding elements of diagonal in vector
        // using add() method
        for (int i = 0; i < 5; i++) {
            v.add(arr[i][i]);
        }
 
        // Sorting elements in vector
        // using Collections.sort() method
        Collections.sort(v);
 
        // Sorted elements are pushed back from vector to
        // row using get() method
        for (int j = 0; j < 5; j++) {
            arr[j][j] = v.get(j);
        }
 
        // Display message
        System.out.println("Matrix after sorting \n");
 
        // Nested for loops to display 2D matrix
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}


 
 

Output

Matrix without sorting 

5 2 0 7 1 
3 4 2 9 14 
5 1 3 5 2 
4 2 6 2 1 
0 6 3 5 1 
Matrix after sorting 

1 2 0 7 1 
3 2 2 9 14 
5 1 3 5 2 
4 2 6 4 1 
0 6 3 5 5 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads