0% found this document useful (0 votes)
2 views3 pages

Java Sorting Algorithm Example

The document contains Java code that defines a 2D integer array with 4 rows and 4 columns and initializes it with values. It then sorts the columns of the array in either ascending or descending order depending on whether the column index is odd or even. The sorted array is then printed out.

Uploaded by

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

Java Sorting Algorithm Example

The document contains Java code that defines a 2D integer array with 4 rows and 4 columns and initializes it with values. It then sorts the columns of the array in either ascending or descending order depending on whether the column index is odd or even. The sorted array is then printed out.

Uploaded by

Alexa Georgescu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROIECT

DUMITRICA ANDRA CRISTINA


FAIMA
GRUPA 1513

import [Link].*;
import [Link].*;
import [Link].*;

/* Name of the class has to be "Main" only if the class is public. */


class Andra
{
public static void main (String[] args) throws [Link]
{
int[][] arr = { {5,13,65,12},
{6,18,27,43},
{33,-14,76,67},
{-1,85,45,75}};
int n=4;
for(int j = 0; j < n ; j++)
{
if(j%2!=0)
{
for (int i = 0; i < n - 1; i++)
{
int index = i;
for (int k = i + 1; k < n; k++)
if (arr[k][j] > arr[index][j])
index = k;

int smallerNumber = arr[index][j];


arr[index][j] = arr[i][j];
arr[i][j] = smallerNumber;
}
}
else

for (int k = 1; k < n; k++)


{
int initial = arr[k][j];
int i = k-1;
while ( (i > -1) && ( arr [i][j] > initial ) ) {
arr [i+1][j] = arr [i][j];
i--;
}
arr[i+1][j] = initial;

}
}

for(int x=0; x<n; x++)


{
for(int y=0; y<n;y++)
[Link](arr[x][y]+" ");
[Link]("\n");
}

}
}

You might also like