Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I need help creating a program in JAVA applications (Row sorting) Implement t

ID: 3913433 • Letter: H

Question

Hi I need help creating a program in JAVA applications

(Row sorting) Implement the follwoing method to sort the rows in a two-dimensional array. A new array is returned and the original array is intact.

public static double[] [] sortRows (double[] [] m)

Write a test program that prompts the user to enter a 3X3 matrix of double values and displays a new row-sorted matrix. Here is a sample run:

Enter a 3-by-3 matrix row by row:

0.15 0.875 0.375

0.55 0.005 0.225

0.30 0.12 0.4

The row-sorted array is

0.15 0.375 0.875

0.005 0.225 0.55

0.12 0.30 0.4

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// ArraySort2D.java

import java.util.Scanner;

public class ArraySort2D {

                /**

                * method to sort the rows of a 2d array

                * @param m

                *            a 2D array of doubles. Assuming this array is not null

                * @return a new sorted 2d array

                */

                public static double[][] sortRows(double[][] m) {

                                /**

                                * creating a new 2d array so that the original array will remain

                                * unchanged

                                */

                                double[][] sorted = new double[m.length][m[0].length];

                                /**

                                * copying the contents from m to new array

                                */

                                for (int i = 0; i < m.length; i++) {

                                                for (int j = 0; j < m[0].length; j++) {

                                                                sorted[i][j] = m[i][j];

                                                }

                                }

                                /**

                                * if you are permitted to use Arrays.copyOf() or System.arrayCopy()

                                * methods, replace the above nested loop with that, much simple and one

                                * line of code

                                */

                               

                                /**

                                * using bubble sort algorithm, sorting each and every row of the array

                                */

                                for (int i = 0; i < sorted.length; i++) {

                                                for (int j = 0; j < sorted[i].length - 1; j++) {

                                                                for (int k = 0; k < sorted[i].length - j - 1; k++) {

                                                                                if (sorted[i][k] > sorted[i][k + 1]) {

                                                                                                // Swapping the numbers

                                                                                                double temp = sorted[i][k];

                                                                                                sorted[i][k] = sorted[i][k + 1];

                                                                                                sorted[i][k + 1] = temp;

                                                                                }

                                                                }

                                                }

                                }

                                //returning sorted array

                                return sorted;

                }

               

                public static void main(String[] args) {

                                Scanner scanner=new Scanner(System.in);

                                //prompting and reading a 3x3 array of doubles

                                System.out.println("Enter a 3-by-3 matrix row by row:");

                                double array[][]=new double[3][3];

                                for(int i=0;i<array.length;i++){

                                                for(int j=0;j<array[i].length;j++){

                                                                array[i][j]=scanner.nextDouble();

                                                }

                                }

                                scanner.nextLine(); //to remove the next line character from stdin

                                //sorting the array

                                double sorted[][]=sortRows(array);

                                //displaying sorted array

                                System.out.println("The row-sorted array is ");

                                for(int i=0;i<sorted.length;i++){

                                                for(int j=0;j<sorted[i].length;j++){

                                                                System.out.print(sorted[i][j]+" ");

                                                }

                                                System.out.println();

                                }

                }

}

/*OUTPUT*/

Enter a 3-by-3 matrix row by row:

0.15 0.875 0.375

0.55 0.005 0.225

0.30 0.12 0.4

The row-sorted array is

0.15 0.375 0.875

0.0050 0.225 0.55

0.12 0.3 0.4