JavaScript Program to Sort the 2D Array Across Rows
Last Updated :
20 Mar, 2024
We will see how to sort the 2D Array across rows using a Javascript program. we can sort the 2D Array Across Rows in many ways including Bubble Sort Algorithm, Array.prototype.sort() method, Insertion Sort Algorithm, Using the Selection Sort Algorithm, and Merge Sort Algorithm.
Example:
Input:
[[8 5 7 2],
[7 3 0 1],
[8 5 3 2],
[9 4 2 1]]
Output:
[[2, 5, 7, 8], [0, 1, 3, 7], [2, 3, 5, 8], [1, 2, 4, 9]]
These are the following approaches:
Using the Bubble Sort Algorithm
In this approach, we use Bubble Sort. First Start iterating through each row of the given 2D array, and sort elements of each row using the Bubble sort sorting algorithm.
Example: Implementation of Sort the 2D Array Across Rows using the Bubble Sort Algorithm
JavaScript
// Function to sort a 2D array
// across rows using Bubble Sort
function sort2DArrayAcrossRows(arr) {
// Iterate through each row of the array
for (let i = 0; i < arr.length; i++) {
// Apply Bubble Sort
// to the current row
for (let j = 0; j < arr[i].length - 1; j++) {
for (let k = 0; k < arr[i].length - j - 1; k++) {
if (arr[i][k] > arr[i][k + 1]) {
// Swap elements if
// they are in the wrong order
let temp = arr[i][k];
arr[i][k] = arr[i][k + 1];
arr[i][k + 1] = temp;
}
}
}
}
return arr;
}
const array2D = [
[4, 2, 6, 1],
[9, 5, 3, 8],
[7, 1, 2, 0]
];
const sortedArray = sort2DArrayAcrossRows(array2D);
console.log(sortedArray);
Output[ [ 1, 2, 4, 6 ], [ 3, 5, 8, 9 ], [ 0, 1, 2, 7 ] ]
Using the Array.prototype.sort() method
In this approach, we use the Array.prototype.sort() method to iterate through each row of the 2D array and apply the sort() method with a custom comparison function to sort the elements in ascending order.
Example: Implementation of Sort the 2D Array Across Rows using the Array.prototype.sort() method
JavaScript
// Function to sort a 2D array across rows
//using Array.prototype.sort()
function sort2DArrayAcrossRows(arr) {
// Iterate through each
// row of the array and sort it
return arr.map(row => row.slice()
.sort((a, b) => a - b));
}
// Example usage:
const array2D = [
[4, 2, 6, 1],
[9, 5, 3, 8],
[7, 1, 2, 0]
];
const sortedArray = sort2DArrayAcrossRows(array2D);
console.log(sortedArray);
Output[ [ 1, 2, 4, 6 ], [ 3, 5, 8, 9 ], [ 0, 1, 2, 7 ] ]
Using the Insertion Sort Algorithm
In this approach, we use the insertion sort algorithm to iterate through each row of the array. Here we apply Insertion Sort to each row, which iteratively places each element in its correct position within the sorted subarray to its left. Continue this process until the entire row is sorted. Repeat this process for each row in the 2D array.
Example: Implementation of Sort the 2D Array Across Rows using the Insertion Sort Algorithm
JavaScript
// Function to sort a 2D array
// across rows using Insertion Sort
function sort2DArrayAcrossRows(arr) {
// Iterate through each row of the array
for (let i = 0; i < arr.length; i++) {
// Apply Insertion Sort to the current row
for (let j = 1; j < arr[i].length; j++) {
let current = arr[i][j];
let k = j - 1;
while (k >= 0 && arr[i][k] > current) {
arr[i][k + 1] = arr[i][k];
k--;
}
arr[i][k + 1] = current;
}
}
return arr;
}
// Example usage:
const array2D = [
[4, 2, 6, 1],
[9, 5, 3, 8],
[7, 1, 2, 0]
];
const sortedArray = sort2DArrayAcrossRows(array2D);
console.log(sortedArray);
Output[ [ 1, 2, 4, 6 ], [ 3, 5, 8, 9 ], [ 0, 1, 2, 7 ] ]
Using the Selection Sort Algorithm
In this approach, we use selection sort to repeatedly selects the minimum element from the unsorted portion of the array and places it at the beginning and by continuing this process until the entire row is sorted. We repeat this process for each row to sort the 2D array.
Example: Implementation of Sort the 2D Array Across Rows using the selection sort algorithm
JavaScript
// Function to sort a 2D array
// across rows using Selection Sort
function sort2DArrayAcrossRows(arr) {
// Iterate through each row of the array
for (let i = 0; i < arr.length; i++) {
// Apply Selection Sort to the current row
for (let j = 0; j < arr[i].length - 1; j++) {
let minIndex = j;
for (let k = j + 1; k < arr[i].length; k++) {
if (arr[i][k] < arr[i][minIndex]) {
minIndex = k;
}
}
if (minIndex !== j) {
let temp = arr[i][j];
arr[i][j] = arr[i][minIndex];
arr[i][minIndex] = temp;
}
}
}
return arr;
}
// Example usage:
const array2D = [
[4, 2, 6, 1],
[9, 5, 3, 8],
[7, 1, 2, 0]
];
const sortedArray = sort2DArrayAcrossRows(array2D);
console.log(sortedArray);
Output[ [ 1, 2, 4, 6 ], [ 3, 5, 8, 9 ], [ 0, 1, 2, 7 ] ]
Using the Merge Sort Algorithm
In this approach, we define functions to merge two sorted arrays and perform Merge Sort on a 1D array. Iterate through each row of the 2D array and apply Merge Sort, which recursively divides the array into halves until each half contains only one element, then merges them back together in sorted order. Repeat this process for each row in the 2D array.
Example: Implementation of Sort the 2D Array Across Rows Using the Merge Sort Algorithm
JavaScript
// Function to merge two sorted arrays
function merge(left, right) {
let result = [];
let leftIndex = 0;
let rightIndex = 0;
while (leftIndex < left.length &&
rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
result.push(left[leftIndex]);
leftIndex++;
} else {
result.push(right[rightIndex]);
rightIndex++;
}
}
return result.concat(left.slice(leftIndex))
.concat(right.slice(rightIndex));
}
// Function to perform merge sort on a 1D array
function mergeSort(arr) {
if (arr.length <= 1) {
return arr;
}
const middle = Math.floor(arr.length / 2);
const left = arr.slice(0, middle);
const right = arr.slice(middle);
return merge(mergeSort(left), mergeSort(right));
}
// Function to sort a 2D array
// across rows using Merge Sort
function sort2DArrayAcrossRows(arr) {
// Iterate through each row of
// the array and apply Merge Sort
return arr.map(row => mergeSort(row));
}
// Example usage:
const array2D = [
[4, 2, 6, 1],
[9, 5, 3, 8],
[7, 1, 2, 0]
];
const sortedArray = sort2DArrayAcrossRows(array2D);
console.log(sortedArray);
Output[ [ 1, 2, 4, 6 ], [ 3, 5, 8, 9 ], [ 0, 1, 2, 7 ] ]
Similar Reads
JavaScript Program for Quick Sort What is Quick Sort Algorithm?Quick sort is one of the sorting algorithms that works on the idea of divide and conquer. It takes an element as a pivot and partitions the given array around that pivot by placing it in the correct position in the sorted array. The pivot element can be selected in the f
4 min read
JavaScript Program for Insertion Sort What is Insertion Sort Algorithm?Insertion sorting is one of the sorting techniques that is based on iterating the array and finding the right position for every element. It compares the current element to the predecessors, if the element is small compare it with the elements before. Move ahead to a
4 min read
JavaScript Program to Find the Distance Value between Two Arrays We will be given two integer arrays and an integer d. The task is to calculate the distance between the two given arrays based on the given integer value. The distance value will be calculated as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <=
3 min read
Javascript Program to Interchange elements of first and last rows in matrix Given a 4 x 4 matrix, we have to interchange the elements of first and last row and show the resulting matrix.Examples : Input : 3 4 5 0 2 6 1 2 2 7 1 2 2 1 1 2Output : 2 1 1 2 2 6 1 2 2 7 1 2 3 4 5 0Input : 9 7 5 1 2 3 4 1 5 6 6 5 1 2 3 1Output : 1 2 3 1 2 3 4 1 5 6 6 5 9 7 5 1The approach is very
2 min read
Java Program to Sort the 2D Array Across Rows This program is used to Sort the 2D array Across rows. We will use the concept of vector to sort each row. Vector 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
3 min read
C Program To Sort 2D Array Across Rows Here, we will see how to sort the 2D Array across rows using a C program: Input: 8 5 7 2 7 3 0 1 8 5 3 2 9 4 2 1 Output: 2 5 7 8 0 1 3 7 2 3 5 8 1 2 4 9Approach: In this approach, we use Bubble Sort. First Start iterating through each row of the given 2D array, and sort elements of each row using th
2 min read
Javascript Program to Sort the matrix row-wise and column-wise Given a n x n matrix. The problem is to sort the matrix row-wise and column-wise.Examples: Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} }Output : 1 3 4 2 5 7 6 8 9Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 2
3 min read
Javascript Program to Sort an array in wave form Given an unsorted array of integers, sort the array into a wave like array. An array 'arr[0..n-1]' is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= .....Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR {
4 min read
Javascript Program for Sort the given matrix Given a n x n matrix. The problem is to sort the given matrix in strict order. Here strict order means that matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row âiâ, where 1 <= i <= n-1, first element of row 'i' is greater than or equal to the la
2 min read
Javascript Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0
5 min read