Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code for Matrix Sort in java #569

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions java/sorting/matrixSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.*;

public class Main {
public static void main(String[] args)
{
// Initialize the 2D vector with some values
List<List<Integer> > v
= new ArrayList<>(Arrays.asList(
new ArrayList<>(Arrays.asList(5, 4, 7)),
new ArrayList<>(Arrays.asList(1, 3, 8)),
new ArrayList<>(Arrays.asList(2, 9, 6))));

int n = v.size();
List<Integer> x = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x.add(v.get(i).get(j));
}
}
Collections.sort(x);
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
v.get(i).set(j, x.get(k++));
}
}

System.out.println("Sorted Matrix Will be:");
for (List<Integer> row : v) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}