Results 1 to 2 of 2
Thread: Sorting a 2dimensional array
 02272012, 03:28 PM #1Member
 Join Date
 Feb 2012
 Location
 Finland
 Posts
 2
 Rep Power
 0
Sorting a 2dimensional 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: "); int row = reader.nextInt(); System.out.print("Number of columns: "); int col = reader.nextInt(); // 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[row1].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[row1].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[row1].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[0].length; col++) { for (int colTwo = col + 1; colTwo < array[0].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[0].length; col++) { System.out.print(array[row][col] + "\t"); } System.out.println(); } } }
Last edited by Krauger; 02272012 at 05:24 PM.
 02272012, 04:29 PM #2Member
 Join Date
 Feb 2012
 Location
 Finland
 Posts
 2
 Rep Power
 0
Re: Sorting a 2dimensional array
Alright... Spent some more time googling and it seems like normal sorting methods can be used for 2dimensional 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[0].length; col++) { for (int colTwo = col + 1; colTwo < array[0].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[0].length; col++) { System.out.print(array[row][col] + "\t"); } System.out.println(); } }
Similar Threads

two dimensional array
By shahnawazzzzzz in forum Java SoftwareReplies: 2Last Post: 10052011, 05:47 AM 
2 dimensional array
By sehudson in forum New To JavaReplies: 5Last Post: 02212011, 12:56 AM 
2 dimensional array help!
By ber1023 in forum New To JavaReplies: 9Last Post: 01022011, 01:29 AM 
Sorting Two Dimensional arrays
By anfielder in forum New To JavaReplies: 5Last Post: 12142010, 08:12 AM 
Help with java sorting two dimensional array
By Joycey in forum New To JavaReplies: 2Last Post: 03272010, 03:36 AM
Bookmarks