-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise08_26.java
71 lines (62 loc) · 2 KB
/
Exercise08_26.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ch_08;
import java.io.DataOutput;
import java.util.*;
/**
* *8.26 (Row sorting) Implement the following method to sort the rows in a
* two-dimensional array.
* <p>
* A new array is returned and the original array is intact.
* public static double[][] sortRows(double[][] m)
* <p>
* <p>
* Write a test program that prompts the user to enter a 3 * 3 matrix of double
* values and displays a new row-sorted matrix.
* <p>
* 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
*/
public class Exercise08_26 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double[][] matrix = new double[3][3];
System.out.println("Enter a 3-by-3 matrix row by row: ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = in.nextDouble();
}
}
double[][] res = sortRows(matrix);
System.out.println("The row-sorted array is: ");
for (int i = 0; i < 3; i++) {
System.out.println(Arrays.toString(res[i]));
}
}
public static double[][] sortRows(double[][] m) {
double[][] sorted = new double[m.length][m.length];
for (int i = 0; i < 3; i++) {
System.arraycopy(m[i], 0, sorted[i], 0, 3);
}
for (int i = 0; i < sorted.length; i++) {
boolean keepSorting = true;
while (keepSorting) {
keepSorting = false;
for (int j = 0; j < sorted[i].length - 1; j++) {
if (sorted[i][j] > sorted[i][j + 1]) {
double tmp = sorted[i][j];
sorted[i][j] = sorted[i][j + 1];
sorted[i][j + 1] = tmp;
keepSorting = true;
}
}
}
}
return sorted;
}
}