Pages

Monday, 29 August 2016

Chapter 8 Exercise 26, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*8.26 (Row sorting) Implement the following 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 3 * 3 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
import java.util.Scanner;
 
public class ProgrammingEx8_26 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.println("Enter a 3-by-3 matrix row by row:");
  double[][] n = new double[3][3];
 
  for (int i = 0; i < n.length; i++) {
   for (int j = 0; j < n[i].length; j++) {
    n[i][j] = input.nextDouble();
   }
  }
 
  double[][] sorted = sortRows(n);
  System.out.println("The original array is:");
  display(n);
  System.out.println("The row-sorted array is:");
  display(sorted);
 }
 
 public static double[][] sortRows(double[][] m) {
  double[][] result = new double[m.length][m[0].length];
  // turning row into single dimension array
  for (int i = 0; i < result.length; i++) {
   result[i] = selectionSort(m[i]);
  }
  return result;
 }
 
 public static double[] selectionSort(double[] inputList) {
  double[] list = inputList.clone();
   
  for (int i = 0; i < list.length - 1; i++) {
   // Find the minimum in the list[i..list.length-1]
   double currentMin = list[i];
   int currentMinIndex = i;
 
   for (int j = i + 1; j < list.length; j++) {
    if (currentMin > list[j]) {
     currentMin = list[j];
     currentMinIndex = j;
    }
   }
 
   // Swap list[i] with list[currentMinIndex] if necessary
   if (currentMinIndex != i) {
    list[currentMinIndex] = list[i];
    list[i] = currentMin;
   }
  }
  return list;
 }
 
 public static void display(double[][] list) {
  for (int i = 0; i < list.length; i++) {
   for (int j = 0; j < list.length; j++) {
    System.out.print(list[i][j] + " ");
   }
   System.out.println();
  }
 }
}

No comments :

Post a Comment