I have this 2d array that sorts the rows but i can't sort the column.. i can't figure it out for the life of me. PLEASE HELP
Please enter size of row between 1 - 20: 5
Please enter size of column between 1 - 20: 5
Your matrix is 5 X 5 array:
The 2D array before the sorting:
105 710 517 769 76
660 817 926 5 702
698 574 420 41 911
767 753 936 191 299
512 256 804 46 946
The 2D array after sorting the row:
769 710 517 105 76
926 817 702 660 5
911 698 574 420 41
936 767 753 299 191
946 804 512 256 46
Code:
import java.util.Scanner; // Scanner is in java.util
public class sortmatrix {
public static void main(String args[]) {
//Create a Scanner
Scanner input = new Scanner(System.in);
//Received one positive integer m from the user
System.out.print("Please enter size of row between 1 - 20: ");
int integerOne = input.nextInt();
while ((integerOne > 20) || (integerOne < 1)){
System.out.print("Please enter size of row between 1 - 20: ");
integerOne = input.nextInt();
}
//Received one positive integer n from the user
System.out.print("Please enter size of column between 1 - 20: ");
int integerTwo = input.nextInt();
while ((integerTwo > 20) || (integerTwo < 1)){
System.out.print("Please enter size of column between 1 - 20: ");
integerTwo = input.nextInt();
}
//Create a m x n array of integers for array2
int [][] array2 = new int [integerOne][integerTwo];
//Display the 2D array elements
System.out.println("Your matrix is " + integerOne + " X " + integerTwo + " array: " );
System.out.println("The 2D array before the sorting: ");
printMatrix(array2);
//Sorting each row in descending order
System.out.println("The 2D array after sorting the row: ");
sort2D(array2);
}
//-------------------------------------------------------------------------------------------------
/*The method for printing the value */
public static void printMatrix (int[][] array2){
/*Generate random integer numbers for this array elements.
Each element number should range between 1 and 999 inclusive. */
for (int row = 0; row < array2.length; row++){
for (int column = 0; column < array2[0].length; column++){
array2[row][column] = (int)(Math.random() * 999) + 1;
//Display the 2D array elements (each row per line).
System.out.print(array2[row][column] + " ");
}
System.out.println();
}
}
//----------------------------------------------------------------------------------
/*binary search */
public static int binarySearch(long[] a, long key) {
int bot = 0;
int top = a.length - 1;
while (bot <= top) {
int mid = bot + (top - bot) / 2;
if (key < a[mid]) top = mid - 1;
else if (key > a[mid]) bot = mid + 1;
else return mid;
}
return -1;
}
//-------------------------------------------------------------------------------------
/* The method for sorting the array elements */
public static void sort2D (int [][] array2){
//Sorting the row of 2D array in descending order
for (int row = 0; row < array2.length; row++){
for(int column = 0; column < array2[0].length; column++) {
for (int columnTwo = column + 1; columnTwo< array2[0].length; columnTwo++){
if(array2[row][columnTwo]>array2[row][column])
{
int temp=array2[row][column];
array2[row][column] = array2[row][columnTwo];
array2[row][columnTwo]=temp;
}
}
}
}
// print sorted array
for (int row = 0; row < array2.length; row++){
for (int column = 0; column < array2[0].length; column++){
System.out.print(array2[row][column] + " ");
}
System.out.println();
}
}
}