Sort Matrix in alternating ascending and descending order rowwise
Last Updated :
23 Feb, 2023
Given an N x N matrix, our task is to print the row of the matrix in ascending and descending order alternatively.
Examples:
Input:
5 7 3 4
9 5 8 2
6 3 8 1
5 8 9 3
Output:
3 4 5 7
9 8 5 2
1 3 6 8
9 8 5 3
Explanation:
Here the first row is sorted in ascending order, second row sorted in descending order, third row sorted in ascending order and so on.
Input:
7 3 4
3 8 2
3 6 1
Output:
3 4 7
8 3 2
1 3 6
Explanation:
Here the first row is sorted in ascending order, second row sorted in descending order, third row sorted in ascending order.
Approach to solve
To solve the problem mentioned above we iterate 0 to N and check if the ith row is even or odd, if it is even then we sort the row in ascending order otherwise sort the ith row in descending order. Return the matrix after N iterations.
Below is the implementation of the above approach:
C++
#include <stdio.h>
#define N 4
void func( int a[][N])
{
for ( int i = 0; i < N; i++) {
if (i % 2 == 0) {
for ( int j = 0; j < N; j++) {
for ( int k = j + 1; k < N; ++k) {
if (a[i][j] > a[i][k]) {
int temp = a[i][j];
a[i][j] = a[i][k];
a[i][k] = temp;
}
}
}
}
else {
for ( int j = 0; j < N; j++) {
for ( int k = j + 1; k < N; ++k) {
if (a[i][j] < a[i][k]) {
int temp = a[i][j];
a[i][j] = a[i][k];
a[i][k] = temp;
}
}
}
}
}
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
printf ( "%d " , a[i][j]);
}
printf ( "\n" );
}
}
int main()
{
int a[N][N] = {
{ 5, 7, 3, 4 },
{ 9, 5, 8, 2 },
{ 6, 3, 8, 1 },
{ 5, 8, 9, 3 }
};
func(a);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int N = 4 ;
static void func( int a[][])
{
int i, j, k;
for (i = 0 ; i < N; i++)
{
if (i % 2 == 0 )
{
for (j = 0 ; j < N; j++)
{
for (k = j + 1 ; k < N; ++k)
{
if (a[i][j] > a[i][k])
{
int temp = a[i][j];
a[i][j] = a[i][k];
a[i][k] = temp;
}
}
}
}
else
{
for (j = 0 ; j < N; j++)
{
for (k = j + 1 ; k < N; ++k)
{
if (a[i][j] < a[i][k])
{
int temp = a[i][j];
a[i][j] = a[i][k];
a[i][k] = temp;
}
}
}
}
}
for (i = 0 ; i < N; i++)
{
for (j = 0 ; j < N; j++)
{
System.out.print(a[i][j] + " " );
}
System.out.print( "\n" );
}
}
public static void main (String []args)
{
int a[][] = { { 5 , 7 , 3 , 4 },
{ 9 , 5 , 8 , 2 },
{ 6 , 3 , 8 , 1 },
{ 5 , 8 , 9 , 3 } };
func(a);
}
}
|
Python3
N = 4
def func(a):
for i in range (N):
if i % 2 = = 0 :
for j in range (N):
for k in range (j + 1 , N):
if a[i][j] > a[i][k]:
temp = a[i][j]
a[i][j] = a[i][k]
a[i][k] = temp
else :
for j in range (N):
for k in range (j + 1 , N):
if a[i][j] < a[i][k]:
temp = a[i][j]
a[i][j] = a[i][k]
a[i][k] = temp
for i in range (N):
for j in range (N):
print (a[i][j], end = " " )
print ()
if __name__ = = '__main__' :
a = [ [ 5 , 7 , 3 , 4 ],
[ 9 , 5 , 8 , 2 ],
[ 6 , 3 , 8 , 1 ],
[ 5 , 8 , 9 , 3 ] ]
func(a)
|
C#
using System;
class GFG{
static int N = 4;
static void func( int [,]a)
{
int i, j, k;
for (i = 0; i < N; i++)
{
if (i % 2 == 0)
{
for (j = 0; j < N; j++)
{
for (k = j + 1; k < N; ++k)
{
if (a[i, j] > a[i, k])
{
int temp = a[i, j];
a[i, j] = a[i, k];
a[i, k] = temp;
}
}
}
}
else
{
for (j = 0; j < N; j++)
{
for (k = j + 1; k < N; ++k)
{
if (a[i, j] < a[i, k])
{
int temp = a[i, j];
a[i, j] = a[i, k];
a[i, k] = temp;
}
}
}
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
Console.Write(a[i, j] + " " );
}
Console.Write( "\n" );
}
}
public static void Main(String []args)
{
int [,]a = { { 5, 7, 3, 4 },
{ 9, 5, 8, 2 },
{ 6, 3, 8, 1 },
{ 5, 8, 9, 3 } };
func(a);
}
}
|
Javascript
<script>
let N = 4;
function func(a)
{
for (let i = 0; i < N; i++) {
if (i % 2 == 0) {
for (let j = 0; j < N; j++) {
for (let k = j + 1; k < N; ++k) {
if (a[i][j] > a[i][k]) {
let temp = a[i][j];
a[i][j] = a[i][k];
a[i][k] = temp;
}
}
}
}
else {
for (let j = 0; j < N; j++) {
for (let k = j + 1; k < N; ++k) {
if (a[i][j] < a[i][k]) {
let temp = a[i][j];
a[i][j] = a[i][k];
a[i][k] = temp;
}
}
}
}
}
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
document.write( " " + a[i][j]);
}
document.write( "<br>" );
}
}
let a = [
[ 5, 7, 3, 4 ],
[ 9, 5, 8, 2 ],
[ 6, 3, 8, 1 ],
[ 5, 8, 9, 3 ]
];
func(a);
</script>
|
Output:
3 4 5 7
9 8 5 2
1 3 6 8
9 8 5 3
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient approach: We can optimize the above approach by sorting any given row by using the sort() function. It will sort any given row in O(n*log(n)) time complexity.
Steps involved in the approach:
- Traverse through each row and check if the row is at an even number or odd.
- If even row is there then sort it in ascending order by directly using the sort function.
- If odd row then sort it in descending order by passing the “greater()” function in sort.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 4
void func( int a[][N])
{
for ( int i = 0; i < N; i++) {
if (i % 2 == 0) {
sort(a[i],a[i]+N);
}
else {
sort(a[i],a[i]+N,greater< int >());
}
}
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
printf ( "%d " , a[i][j]);
}
printf ( "\n" );
}
}
int main()
{
int a[N][N] = {
{ 5, 7, 3, 4 },
{ 9, 5, 8, 2 },
{ 6, 3, 8, 1 },
{ 5, 8, 9, 3 }
};
func(a);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
public class Main {
public static void func(Integer[][] a) {
for ( int i = 0 ; i < a.length; i++) {
if (i % 2 == 0 ) {
Arrays.sort(a[i]);
} else {
Arrays.sort(a[i], Collections.reverseOrder());
}
}
for ( int i = 0 ; i < a.length; i++) {
for ( int j = 0 ; j < a[i].length; j++) {
System.out.print(a[i][j] + " " );
}
System.out.println();
}
}
public static void main(String[] args) {
Integer[][] a = {{ 5 , 7 , 3 , 4 }, { 9 , 5 , 8 , 2 }, { 6 , 3 , 8 , 1 }, { 5 , 8 , 9 , 3 }};
func(a);
}
}
|
Python
import numpy as np
def func(a):
for i in range ( len (a)):
if i % 2 = = 0 :
a[i] = np.sort(a[i])
else :
a[i] = np.sort(a[i])[:: - 1 ]
for i in range ( len (a)):
for j in range ( len (a[i])):
print (a[i][j])
print ()
a = np.array([[ 5 , 7 , 3 , 4 ], [ 9 , 5 , 8 , 2 ], [ 6 , 3 , 8 , 1 ], [ 5 , 8 , 9 , 3 ]])
func(a)
|
Javascript
function func(a) {
for (let i = 0; i < a.length; i++) {
if (i % 2 === 0) {
a[i].sort((a, b) => a - b);
} else {
a[i].sort((a, b) => b - a);
}
}
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a[i].length; j++) {
console.log(a[i][j]);
}
console.log( "\n" );
}
}
let a = [[5, 7, 3, 4], [9, 5, 8, 2], [6, 3, 8, 1], [5, 8, 9, 3]];
func(a);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class MainClass {
public static void func( int [][] a) {
for ( int i = 0; i < a.Length; i++) {
if (i % 2 == 0) {
Array.Sort(a[i]);
} else {
Array.Sort(a[i], new Comparison< int >((x, y) => y.CompareTo(x)));
}
}
for ( int i = 0; i < a.Length; i++) {
for ( int j = 0; j < a[i].Length; j++) {
Console.Write(a[i][j] + " " );
}
Console.WriteLine();
}
}
public static void Main ( string [] args) {
int [][] a = new int [][] {
new int [] {5, 7, 3, 4},
new int [] {9, 5, 8, 2},
new int [] {6, 3, 8, 1},
new int [] {5, 8, 9, 3}
};
func(a);
}
}
|
Output
3 4 5 7
9 8 5 2
1 3 6 8
9 8 5 3
Time Complexity: O(N2log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...