# Thread: Sorting a 2-dimensional array

1. Member Join Date
Feb 2012
Location
Finland
Posts
2
Rep Power
0

## Sorting a 2-dimensional array

EDIT: As I mentioned in the second post, I found the correct bubblesort method for sorting my array. I updated the code of this post to what I currently have. It seems to work but I don't mind someone taking a look whether I've done everything correctly.

Hello, first time poster here. I have recently started learning Java and it's been pretty interesting so far. I just tend to get stuck on (usually) small problems, which is getting pretty time consuming. I have been googling for tips, but figured it might be a good idea to start posting on some dedicated forum.

Anyway, to the problem: I have a program that asks the user how many rows and columns he wants to print. Then the program prints the corresponding matrix and counts the average of all elements. After that it counts the average of each row and then the average of rows (basically the same number as the previous average). Then the same operation is done for columns.

I think I have gotten it correct this far, but the last step is to sort each row in an ascending order. I'm not quite sure how I should approach this. I was thinking about adding every row into a normal array and then sort it. But it feels sort of complicated, so I was thinking if this is the correct way...

Here's the code, feel free to check if the previous tasks are implemented correctly, I'm not 100% sure about them:

Java Code:
```import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Matrix {

private static final Scanner reader = new Scanner(System.in);
private static Random rand = new Random();
public static final int MAX = 20;
public static final int MIN = -20;

public static void main(String[] args) {

System.out.print("Number of rows: ");
System.out.print("Number of columns: ");

// Create a matrix and print it.
int[][] matrix = createMatrix(row, col);
System.out.println("Formed matrix: ");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++)
System.out.print(matrix[i][j] + "\t");
System.out.println();
}

// Count averages.
double normalAverage = matrixAverage(matrix, row, col);
double rowAverage = rowAverage(matrix, row, col);
double colAverage = colAverage(matrix, row, col);

// Print averages.
System.out.printf("----------------------------------------------\n");
System.out.printf("Average of every element: %.2f\n", normalAverage);
System.out.printf("Average of row averages: %.2f\n", rowAverage);
System.out.printf("Average of column averages: %.2f\n", colAverage);
System.out.printf("----------------------------------------------\n");

// Sort rows in an ascending order.
System.out.println("Rows in an ascending order: ");
sortMatrix(matrix);

}
public static int[][] createMatrix(int row, int col) {
int[][] matrix = new int[row][col];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++)
matrix[i][j] = MIN + rand.nextInt(MAX + 1 - MIN);
}
return matrix;
}
public static double matrixAverage(int[][] matrix, int row, int col) {
double sum = 0.0;
double average = 0.0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[row-1].length; j++)
sum += matrix[i][j];
}
average = sum / (row * col);
return average;
}
public static double rowAverage(int[][] matrix, int row, int col) {
double sum = 0.0;
double rowAverage = 0.0;
double average = 0.0;
for (int i = 0; i < matrix.length; i++) {
sum = 0.0;
for (int j = 0; j < matrix[row-1].length; j++) {
sum += matrix[i][j];
}
rowAverage += sum / col;
}
average = rowAverage / row;
return average;
}
public static double colAverage(int[][] matrix, int row, int col) {
double sum = 0.0;
double colAverage = 0.0;
double average = 0.0;
for (int j = 0; j < matrix[row-1].length; j++) {
sum = 0.0;
for (int i = 0; i < matrix.length; i++) {
sum += matrix[i][j];
}
colAverage += sum / row;
}
average = colAverage / col;
return average;
}
public static void swap(int[][] array, int row, int col, int colTwo) {
int temp = array[row][col];
array[row][col] = array[row][colTwo];
array[row][colTwo] = temp;
}
public static void sortMatrix(int[][] array) {
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array.length; col++) {
for (int colTwo = col + 1; colTwo < array.length; colTwo++) {
if (array[row][colTwo] < array[row][col])
swap(array, row, col, colTwo);
}
}
}
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array.length; col++) {
System.out.print(array[row][col] + "\t");
}
System.out.println();
}
}
}```
Last edited by Krauger; 02-27-2012 at 04:24 PM.  Reply With Quote

2. Member Join Date
Feb 2012
Location
Finland
Posts
2
Rep Power
0

## Re: Sorting a 2-dimensional array

Alright... Spent some more time googling and it seems like normal sorting methods can be used for 2-dimensional arrays with some modifications.

Java Code:
```public static void sortMatrix(int[][] array) {
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array.length; col++) {
for (int colTwo = col + 1; colTwo < array.length; colTwo++) {
if(array[row][colTwo] > array[row][col])
{
int temp = array[row][col];
array[row][col] = array[row][colTwo];
array[row][colTwo] = temp;
}
}
}
}
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array.length; col++) {
System.out.print(array[row][col] + "\t");
}
System.out.println();
}
}```
This seems to do the trick, in the wrong order (descending) but I'll try to fix it now.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•