/ Published in: Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import java.util.Arrays; public class A { public void bubblesort(double[] x) { for (int i = 0; i < x.length; i++) { for (int j = x.length - 1; j > 0; j--) { if (x[j - 1] > x[j]) { double tmp = x[j - 1]; x[j - 1] = x[j]; x[j] = tmp; } } } } public void decreasingInsertionSort(double[] x) { for (int i = 1; i < x.length; i++) { double max = x[i]; int j = i; while (j > 0 && x[j - 1] < max) { // change x[j - 1] > max to x[j - 1] < max x[j] = x[j - 1]; j--; } x[j] = max; } } public void employees() { int[][] employees = {{0, 2, 4, 3, 4, 5, 8, 8}, {1, 7, 3, 4, 3, 3, 4, 4}, {2, 3, 3, 4, 3, 3, 2, 2}, {3, 9, 3, 4, 7, 3, 4, 1}, {4, 3, 5, 4, 3, 6, 3, 8}, {5, 3, 4, 4, 6, 3, 4, 4}, {6, 3, 7, 4, 8, 3, 8, 4}, {7, 6, 3, 5, 9, 2, 7, 9}}; int[] sum = new int[8]; for (int i = 0; i < employees.length; i++) { int no = employees[i][0]; for (int j = 1; j < employees[i].length; j++) { sum[no] += employees[i][j]; } } for (int i = 0; i < employees.length; i++) { for (int j = employees.length - 1; j > 0; j--) { if (sum[employees[j - 1][0]] < sum[employees[j][0]]) { int[] tmp = employees[j - 1]; employees[j - 1] = employees[j]; employees[j] = tmp; } } } } for (int i = 0; i < employees.length; i++) { str += "Employee " + employees[i][0] + "\t"; for (int j = 1; j < employees[i].length; j++) { str += employees[i][j] + " "; } str += sum[employees[i][0]] + "\n"; } return str; } // 6.18 A a = new A(); double[] x = {6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5}; a.bubblesort(x); // 6.19 double[] y = {6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5}; a.decreasingInsertionSort(y); // 6.23 a.employees(); } }