JavaScript program for Sort the given matrix

Sorting a given matrix using JavaScript is a fundamental operation in programming. Sorting is the process of arranging the elements of a collection or matrix in a particular order. It is essential to make searching and other operations more efficient.

In this article, we will discuss how to sort a given matrix using the JavaScript programming language.


Given a n x n matrix in a specific order known as the "strict order". In the strict order, each row of the matrix must be sorted in ascending order, and for any row i where 1 <= i <= n-1, the first element of that row must be greater than or equal to the last element of the previous row (i-1).

Input: mat[] =   [5, 4, 7]
   [1 3, 8]
   [2, 9, 6]
Output: mat[]    [1, 2, 3]
   [4, 5, 6] 
   [7, 8, 9]


To sort a given matrix in JavaScript, we will use a sorting algorithm called the "bubble sort." The bubble sort algorithm is a simple algorithm that repeatedly steps through the list or matrix, compares adjacent elements, and swaps them if they are in the wrong order. The algorithm gets its name from the way smaller elements bubble to the top of the list or matrix.


STEP 1 − Start iterating over the rows of the matrix from the first row to the second last row.

STEP 2 − For each row, iterate over the columns of the matrix from the first column to the second last column.

STEP 3 − For each cell, compare it with the cell to its right and below it.

STEP 4 − If the cell to the right or below it is smaller than the current cell, swap the two cells.

STEP 5 − Continue iterating until the end of the matrix is reached.

STEP 6 − Repeat the above process until no swaps are made during an iteration.

STEP 7 − The matrix is now sorted.


We are going to use the bubble sort algorithm for the implementation of the above algorithm using JavaScript. The program takes in a two-dimensional array called a matrix, where each sub-array represents a row of the matrix. The program sorts the elements in the matrix in ascending order using the bubble sort algorithm.

Bubble sort works by comparing adjacent elements in the array and swapping them if they are in the wrong order. The algorithm repeats this process until the entire array is sorted. In this program, two nested loops are used to traverse the matrix and compare adjacent elements.

The program first initializes two variables called 'rows' and 'cols' to store the number of rows and columns in the matrix respectively. It then sets up a do-while loop that will continue iterating until no more swaps are made.

Within the loop, two nested for-loops traverse the matrix and compare adjacent elements. If two adjacent elements are in the wrong order, they are swapped, and the 'swapped' variable is set to true. If no swaps are made, the loop terminates.

The program then returns the sorted matrix. The program is tested by passing in an unsorted matrix to the 'bubbleSort' function, sorting it, and then printing both the unsorted and sorted matrices to the console. Now let’s write the code and execute it!


function bubbleSort(matrix) {
   let rows = matrix.length;
   let cols = matrix[0].length;
   let swapped;
   do {
      swapped = false;
      for (let i = 0; i < rows - 1; i++) {
         for (let j = 0; j < cols - 1; j++) {
            if (matrix[i][j] > matrix[i][j + 1]) {
               let temp = matrix[i][j];
               matrix[i][j] = matrix[i][j + 1];
               matrix[i][j + 1] = temp;
               swapped = true;
            if (matrix[i][j] > matrix[i + 1][j]) {
               let temp = matrix[i][j];
               matrix[i][j] = matrix[i + 1][j];
               matrix[i + 1][j] = temp;
               swapped = true;
   } while (swapped);
   return matrix;
// Testing the function
let matrix = [
   [5, 4, 3],
   [2, 1, 0],
   [8, 7, 6]
console.log("Unsorted Matrix:");
for (let i = 0; i < matrix.length; i++) {
matrix = bubbleSort(matrix);
console.log("Sorted Matrix:");
for (let i = 0; i < matrix.length; i++) {


To sum up, we have discussed how to sort a given matrix using the bubble sort algorithm in JavaScript. The program sorts the matrix by comparing adjacent elements and swapping them if necessary until the entire matrix is sorted. The program is flexible and can handle matrices of different sizes. However, for larger datasets, more efficient sorting algorithms like merge sort or quicksort are recommended due to their faster time complexities. Nonetheless, the bubble sort algorithm remains a useful technique for learning sorting algorithms and is an essential concept for programming beginners.

Updated on: 17-Apr-2023


Kickstart Your Career

Get certified by completing the course

Get Started