Java Program to Sort 2D Array Across Columns
Last Updated :
18 Mar, 2022
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 the 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 Columns. We will use the concept of vector to sort each column.
Algorithm:
- Traverse each column one by one.
- Add elements of Column 1 in vector v.
- Sort the vector.
- Push back the sorted elements from vector to column.
- Empty the vector by removing all elements for fresh sorting.
- Repeat the above steps until all columns are done.
Illustration: Creating a Vector
Here we are creating a default vector of the initial capacity is 10 so do the syntax is as follows:
Vector<E> v = new Vector<E>();
Functions that will be used in order to achieve the goal are as follows:
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(): This method is used to add elements in the vector.
Syntax:
Vector.add(value)
D. get(): This method will get the element of Vector stored at a particular index
Syntax:
Vector.get(3);
Example
Java
// Java Program to Sort 2D array across Columns
// 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 for 2D array
int[][] arr = { { 7, 2, 0, 5, 1 },
{ 3, 8, 2, 9, 14 },
{ 5, 1, 0, 5, 2 },
{ 4, 2, 6, 0, 1 } };
// Display message for better readability
System.out.println("Matrix without sorting \n");
// Nested iteration to display matrix
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
// Printing elements of 2D matrix
System.out.print(arr[i][j] + " ");
}
// New line as we are done with row
System.out.println();
}
// Creating an object of Vector class
Vector<Integer> v = new Vector<>();
// Nested iteration using for loops
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
// Adding elements of columns in vector
// using add() method
v.add(arr[j][i]);
}
// Sorting elements in vector
// using sort() method
Collections.sort(v);
for (int j = 0; j < 4; j++) {
// Sorted elements are pushed back
// from vector to column
arr[j][i] = v.get(j);
}
// Elements are removed from vector for
// fresh sorting using remove() method
v.removeAll(v);
}
// Printing matrix after sorting
System.out.println("Matrix after sorting \n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
OutputMatrix without sorting
7 2 0 5 1
3 8 2 9 14
5 1 0 5 2
4 2 6 0 1
Matrix after sorting
3 1 0 0 1
4 2 0 5 1
5 2 2 5 2
7 8 6 9 14
Auxiliary Space : O(1)
Similar Reads
Java Program to Sort 2D Array Across Left Diagonal 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 th
3 min read
Java Program to Sort the 2D Array Across Rows This program is used to Sort the 2D array Across rows. We will use the concept of vector to sort each row. Vector Vector(): Creates a default vector of the initial capacity is 10. Vector<E> v = new Vector<E>(); Functions we will use in this: 1. removeAll(): The java.util.vector.removeAll
3 min read
Java Program to Sort an array in wave form Given an unsorted array of integers, sort the array into a wave-like array. An array 'arr[0..n-1]' is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ..... Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR
4 min read
Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort()
2 min read
Print a 2D Array or Matrix in Java In this article, we will learn to Print 2 Dimensional Matrix. 2D-Matrix or Array is a combination of Multiple 1 Dimensional Arrays. In this article we cover different methods to print 2D Array. When we print each element of the 2D array we have to iterate each element so the minimum time complexity
4 min read
Java Program to Sort the Elements of an Array in Descending Order Here, we will sort the array in descending order to arrange elements from largest to smallest. The simple solution is to use Collections.reverseOrder() method. Another way is sorting in ascending order and reversing.1. Using Collections.reverseOrder()In this example, we will use Collections.reverseO
2 min read