### java

#### Sorting a nX3 matrix (Most efficient way)

```I am writing code for Mo's algorithm with update in JAVA , so in this we have to order in ascending order an ArrayList of N X 3 matrix in this way
The ArrayList is of the form
Ai,Bi,Ci
For example:
1,2,3
2,3,4
1,2,1
2,3,5
so to sort them , first look for all the Ai, if there is a clash like in Ai of 1,2,3 and 1,2,1 ,then look for Bi,if there is a clash ,then look for Ci, and sort them accordingly .
Sorted array
1,2,1
1,2,3
2,3,4
2,3,5
So is there any data structure to sort them without having to write a big long piece of code ?
// RIGHT NOW I HAVEN'T MADE AN N X # MATRIX ,WHAT I HAVE DONE IS TAKEN 3 ARRAYLISTS , yes I WILL CHANGE IT INTO A SINGLE ARRAYLIST OF SIZE N X 3
public static void main(String[] args) throws IOException {
//finding no of distict numbers
String str;
//Taking the Array Now
ArrayList<Integer> arr=new ArrayList<Integer>();
StringTokenizer st = new StringTokenizer(str," ");
while(st.hasMoreTokens())
{
}
// Total q queries including updates + finding distinct numbers
int Q;
Q = Integer.parseInt(str);
int[] q1=new int[Q];
int[] q2=new int[Q];
int[] q3=new int[Q];
int[] q4=new int[Q];
ArrayList<Integer> query1=new ArrayList<Integer>();// RIGHT NOW I HAVEN'T MADE AN N X # MATRIX ,WHAT I HAVE DONE IS TAKEN 3 ARRAYLISTS , yes I WILL CHANGE IT INTO A SINGLE ARRAYLIST OF SIZE N X 3
ArrayList<Integer> query2=new ArrayList<Integer>();// RIGHT NOW I HAVEN'T MADE AN N X # MATRIX ,WHAT I HAVE DONE IS TAKEN 3 ARRAYLISTS , yes I WILL CHANGE IT INTO A SINGLE ARRAYLIST OF SIZE N X 3
ArrayList<Integer> query3=new ArrayList<Integer>();// RIGHT NOW I HAVEN'T MADE AN N X # MATRIX ,WHAT I HAVE DONE IS TAKEN 3 ARRAYLISTS , yes I WILL CHANGE IT INTO A SINGLE ARRAYLIST OF SIZE N X 3
ArrayList<Integer> update1=new ArrayList<Integer>();
ArrayList<Integer> update2=new ArrayList<Integer>();
ArrayList<Integer> update3=new ArrayList<Integer>();
//int[] update1=new int[Q];
for(int i2=0;i2<Q;i2++)
{
StringTokenizer st2 = new StringTokenizer(str," ");
q1[i2]=Integer.parseInt(st2.nextToken());
q2[i2]=Integer.parseInt(st2.nextToken());
q3[i2]=Integer.parseInt(st2.nextToken());
if(q1[i2]==1)
{
}
else
{
}
}
}
}
LAST QUERY-->
IN Mo's Algorithm with updates, we arrange the ArrayList by
int sqrt = Math.sqrt(N) // N is the length of ArrayList containing all the
// numbers from which range queries have to be told
now sort the . numbers as
Now let's say L,R,time are for eg
1,3,3
2,3,4
2,1,1
2,3,5
So sorting them by L,R,time returns result as
Sorted array
1,3,3
2,1,1
2,3,4
2,3,5
Now sorting them by has to be done ,how can be that done ? the final output has to be like without square root that is the values L,R,time should not change but sorting has to be done with . How to achieve that -- ?
The final output may look like this depending on the value of N
2,1,1
1,3,3 //Very Imp see this it is sorted
2,3,4
2,3,5
```
```Radix sort
You can use default sort method of collections
Collections.sort(yourArrayN2, comparator);
You should define comparator which compares both arrays starting from first elements (we assume that these arrays / lists have the same size; otherwise you will modify the comparator).
Comparator:
public class ListComparator implements Comparator<List<Integer>> {
#Override
public int compare(List<Integer> list1, List<Integer> list2) {
if (list1 == null || list2 == null || list1 == list2) {
throw new IllegalArgumentException();
}
int size = list1.size();
if (size != list2.size()) {
throw new IllegalArgumentException();
}
for (int i = 0; i < size; i++) {
int delta = list1.get(i) - list2.get(i);
if (delta != 0) {
return delta;
}
}
return 0;
}
}
And its usage
public class App {
public void execute() {
List<List<Integer>> list2D = make2DList();
printList2D(list2D);
System.out.println("\n");
Collections.sort(list2D, new ListComparator());
printList2D(list2D);
}
//This is where you create your 2D list. It can be read from file, etc.
private List<List<Integer>> make2DList() {
List<List<Integer>> res = new ArrayList<>(3);
return res;
}
private List<Integer> makeList(Integer ... numbers) {
List<Integer> res = new ArrayList<>();
for (Integer i : numbers) {
}
return res;
}
private void printList2D(List<List<Integer>> list2D) {
for (List<Integer> list : list2D) {
printList(list);
}
}
private void printList(List<Integer> list) {
int size = list.size();
int lastIndex = size - 1;
for (int i = 0; i < lastIndex; i++) {
System.out.print(list.get(i) + ", ");
}
System.out.print(list.get(lastIndex) + "\n");
}
}
It prints
1, 2, 3
2, 3, 4
1, 2, 1
2, 3, 5
1, 2, 1
1, 2, 3
2, 3, 4
2, 3, 5```

Encrypt Message

code
soft
python
ios
c
html
jquery
cloud
mobile