This program is used to Sort the 2D array Across rows. We will use the concept of vector to sort each row.
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(Collection col) method is used to remove all the elements from the vector, present in the collection specified.
2. Collections.sort(): This method is used to sort the vector.
3. add(): This adds up elements in the vector.
4. get(): This method will get an element of Vector stored at a particular index.
- Traverse each row one by one.
- Add elements of Row 1 in vector v.
- Sort the vector.
- Push back the sorted elements from vector to row.
- Empty the vector by removing all elements for fresh sorting.
- Repeat the above steps until all rows are done.
Matrix without sorting 1 8 4 7 3 8 3 1 7 5 6 2 0 7 1 2 6 4 1 9 Matrix after sorting 1 3 4 7 8 1 3 5 7 8 0 1 2 6 7 1 2 4 6 9
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.