Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views5 pages

Sorting Each Row and Column in 2d Array

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Sorting Each Row and Column in 2d Array

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SORTING EACH ROW AND COLUMN IN 2D ARRAY

import java.util.*;
class Mix_Sort
{
int a[][],m,n;
Mix_Sort(int row,int col)
{
m=row;
n=col;
a=new int[m][n];
}

void input()
{
Scanner sc=new Scanner(System.in);
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
}

void ascEven(int x[])


{
int i,j;
for(i=1;i<x.length;i++)
{
j=i-1;
int temp=x[i];
while(j>=0 && x[j]>temp)
{
x[j+1]=x[j];
j--;
}
x[j+1]=temp;
}
}

void dscOdd(int x[])


{
int i,j;
for(i=1;i<x.length;i++)
{
j=i-1;
int temp=x[i];
while(j>=0 && x[j]<temp)
{
x[j+1]=x[j];
j--;
}
x[j+1]=temp;
}
}

void arrange()
{
int i;
for(i=0;i<m;i++)
{
if(i%2==0)
dscOdd(a[i]);
else
ascEven(a[i]);
}
}

void display()
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}

public static void main()


{
Mix_Sort ob=new Mix_Sort(3,3);
ob.input();
ob.arrange();
ob.display();
}
}
import java.util.*;
class Sort
{
int a[][],m,n;
Sort(int row,int col)
{
m=row;
n=col;
a=new int[m][n];
}
void arrange(int x[])
{
int i,j,temp;
for(i=1;i<x.length;i++)
{
j=i-1;
temp=x[i];
while(j>=0 && x[j]>temp)
{
x[j+1]=x[j];
j--;
}
x[j+1]=temp;
}
}
void sorting()
{
int i,j;
for(i=0;i<m;i++)
{
arrange(a[i]);
}
}
void input()
{
Scanner sc=new Scanner(System.in);
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
}

void display()
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}

public static void main()


{
Sort ob=new Sort(3,3);
ob.input();
ob.sorting();
ob.display();
}
}
import java.util.*;
class SortColumn
{
int a[][],m,n;
SortColumn(int row,int col)
{
m=row;
n=col;
a=new int[m][n];
}

void arrange(int x[])


{
int i,j,temp;
for(i=1;i<x.length;i++)
{
j=i-1;
temp=x[i];
while(j>=0 && x[j]>temp)
{
x[j+1]=x[j];
j--;
}
x[j+1]=temp;
}
}

void sorting()
{
int i,j;
for(i=0;i<m;i++)
{
int c[]=new int[m];
for(j=0;j<n;j++)
{
c[j]=a[j][i];
}
arrange(c);
for(j=0;j<n;j++)
{
a[j][i]=c[j];
}
}
}

void input()
{
Scanner sc=new Scanner(System.in);
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
}

void display()
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}

public static void main()


{
SortColumn ob=new SortColumn(3,3);
ob.input();
ob.sorting();
ob.display();
}
}

You might also like