search
HomeJavajavaTutorial2D Array Sorting in Java

The following article provides an outline for 2D Array Sorting in Java. An array of arrays can be a two-dimensional array. The matrices that make up the 2D array represent a collection of rows and columns. Because the elements of 2D arrays can get accessed at random, we can access the individual cells in a 2D array using their indexes, just like we can with one-dimensional arrays.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

In a 2D array, a cell has two indexes one is its row number, and the other is its column number. Sorting is a technique for arranging elements in a 2D array in a specific order. The 2D array can be in either ascending or descending order. Let’s see how to sort different ways the 2D array in Java in ascending and descending order.

Examples of 2D Array Sorting in Java

Different examples are as below:

Example #1

Example for 2D array sorting in Java to sort all elements of a 2D Array.

Code:

package jex;
import java.util.*;
public class demo {
// using bubble sort to sort 2D array
// sort 2D array same as it is in a 1D array of size n * m
public static void sort(int arr[][]) {
int i, j, temp;
int n=arr.length;
int m=arr[0].length;
for (i = 0; i  arr[(j + 1) / m][(j + 1) % m]) {
temp = arr[(j + 1) / m][(j + 1) % m];
arr[(j + 1) / m][(j + 1) % m] = arr[j / m][j % m];
arr[j / m][j % m] = temp;
}
}
}
}
public static void print(int arr[][]) {
int i, j;
int n=arr.length;
int m=arr[0].length;
for (i = 0; i 
<p><strong>Output:</strong></p>
<p><img  src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500285192899.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="2D Array Sorting in Java" ></p>
<p>As in the above program, the sort() method is useful to iterate each element of a 2D array, and when the current element is greater than the next element, then swap the numbers. Finally, the print method displays all the elements of the 2D array. In the main function, the 2D array is created and printed after and before calling the sort() function, as shown in the above output.</p>
<h4 id="Example">Example #2</h4>
<p>Example for 2D array sorting in Java to sort all elements of a 2D array by column-wise.</p>
<p><strong>Code:</strong></p>
<pre class="brush:php;toolbar:false">package jex;
import java.util.*;
public class demo {
public static void sort(int arr[][]) {
int i, j,k, temp;
int n=arr.length;
int m=arr[0].length;
for (k = 0; k  arr[j + 1][k]) {
temp = arr[j + 1][k];
arr[j + 1][k] = arr[j][k];
arr[j][k] = temp;
}
}
}
}
}
public static void print(int arr[][]) {
int i, j;
int n=arr.length;
int m=arr[0].length;
for (i = 0; i 
<p><strong>Output:</strong></p>
<p><img  src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500285232319.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="2D Array Sorting in Java" ></p>
<p>As in the above rewrite program, the sort() method is useful to iterate each element of a 2D array and sort the array column-wise. Finally, the print method displays all the elements of the 2D array. In the main function, the 2D array is created and printed after and before calling the sort() function, as shown in the above output.</p>
<h4 id="Example">Example #3</h4>
<p>To sort all elements of a 2D array by row-wise.</p>
<p><strong>Code:</strong></p>
<pre class="brush:php;toolbar:false">package jex;
import java.util.*;
public class demo {
// using bubble sort to sort 2D array
// sort 2D array same as it is in a 1D array of size n * m
public static void sort(int arr[][]) {
int i, j,k, temp;
int n=arr.length;
int m=arr[0].length;
for(k=0;k<n applying bubble sort on kth row for if>arr[k][j+1]){
temp = arr[k][j+1];
arr[k][j+1] = arr[k][j];
arr[k][j] = temp;
}
}
}
}
}
public static void print(int arr[][]) {
int i, j;
int n=arr.length;
int m=arr[0].length;
for (i = 0; i 
<p><strong>Output:</strong></p>
<p><img  src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500285549179.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="2D Array Sorting in Java" ></p>
<p>As in the above rewrite program, the sort() method is useful to iterate each element of a 2D array and sort the array row-wise. Finally, the print method displays all the elements of the 2D array. In the main function, the 2D array is created and printed after and before calling the sort() function, as shown in the above output.</p>
<h3 id="Conclusion">Conclusion</h3>
<p>Sorting is a technique for arranging elements in a 2D array in a specific order. For example, in a 2D array, a cell has two indexes: its row number and its column number.</p></n>

The above is the detailed content of 2D Array Sorting in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does platform independence benefit enterprise-level Java applications?How does platform independence benefit enterprise-level Java applications?May 03, 2025 am 12:23 AM

Java is widely used in enterprise-level applications because of its platform independence. 1) Platform independence is implemented through Java virtual machine (JVM), so that the code can run on any platform that supports Java. 2) It simplifies cross-platform deployment and development processes, providing greater flexibility and scalability. 3) However, it is necessary to pay attention to performance differences and third-party library compatibility and adopt best practices such as using pure Java code and cross-platform testing.

What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?May 03, 2025 am 12:22 AM

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.May 03, 2025 am 12:21 AM

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

What are the benefits of Java's platform independence for developers?What are the benefits of Java's platform independence for developers?May 03, 2025 am 12:15 AM

Java'splatformindependenceissignificantbecauseitallowsdeveloperstowritecodeonceandrunitonanyplatformwithaJVM.This"writeonce,runanywhere"(WORA)approachoffers:1)Cross-platformcompatibility,enablingdeploymentacrossdifferentOSwithoutissues;2)Re

What are the advantages of using Java for web applications that need to run on different servers?What are the advantages of using Java for web applications that need to run on different servers?May 03, 2025 am 12:13 AM

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?May 02, 2025 am 12:25 AM

JVM implements the WORA features of Java through bytecode interpretation, platform-independent APIs and dynamic class loading: 1. Bytecode is interpreted as machine code to ensure cross-platform operation; 2. Standard API abstract operating system differences; 3. Classes are loaded dynamically at runtime to ensure consistency.

How do newer versions of Java address platform-specific issues?How do newer versions of Java address platform-specific issues?May 02, 2025 am 12:18 AM

The latest version of Java effectively solves platform-specific problems through JVM optimization, standard library improvements and third-party library support. 1) JVM optimization, such as Java11's ZGC improves garbage collection performance. 2) Standard library improvements, such as Java9's module system reducing platform-related problems. 3) Third-party libraries provide platform-optimized versions, such as OpenCV.

Explain the process of bytecode verification performed by the JVM.Explain the process of bytecode verification performed by the JVM.May 02, 2025 am 12:18 AM

The JVM's bytecode verification process includes four key steps: 1) Check whether the class file format complies with the specifications, 2) Verify the validity and correctness of the bytecode instructions, 3) Perform data flow analysis to ensure type safety, and 4) Balancing the thoroughness and performance of verification. Through these steps, the JVM ensures that only secure, correct bytecode is executed, thereby protecting the integrity and security of the program.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function