![]() |
VOOZH | about |
We are given a 2D array of order N X M and a column number K ( 1<=K<=m). Our task is to sort the 2D array according to values in Column K.
Examples:
Input: If our 2D array is given as (Order 4X4)
39 27 11 42
10 93 91 90
54 78 56 89
24 64 20 65Sorting it by values in column 3
Output:
39 27 11 42
24 64 20 65
54 78 56 89
10 93 91 90
39 27 11 42 24 64 20 65 54 78 56 89 10 93 91 90
The idea is to use Arrays.sort in Java.
Syntax:
Arrays.sort(arr, (a,b)->a[0]-b[0]);Example:
39 27 11 42 24 64 20 65 54 78 56 89 10 93 91 90
Time complexity: O(n log n) where n is the number of rows.