Revision: 41921
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 24, 2011 23:10 by attomos
Initial Code
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];
}
}
System.out.println(printEmployees(employees, sum));
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;
}
}
}
System.out.println(printEmployees(employees, sum));
}
public String printEmployees(int[][] employees, int[] sum) {
String str = "\n \tSu M T W H F Sa Sum\n\n";
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;
}
public static void main(String []args) {
// 6.18
A a = new A();
double[] x = {6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5};
System.out.println(Arrays.toString(x));
a.bubblesort(x);
System.out.println(Arrays.toString(x));
// 6.19
double[] y = {6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5};
System.out.println(Arrays.toString(y));
a.decreasingInsertionSort(y);
System.out.println(Arrays.toString(y));
// 6.23
a.employees();
}
}
Initial URL
Initial Description
Initial Title
Java 1 - Arrays
Initial Tags
Initial Language
Java