import java.util.*;
public class Example {
public static void main(String[] args) {
int[][] hours = {
{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}
};
int[] weeklyHrs = new int[hours.length];
for (int i = 0; i < hours.length; i++) {
for (int j = 0; j < hours[i].length; j++) {
weeklyHrs[i] = weeklyHrs[i] + hours[i][j];
}
}
for (int i = 0; i < weeklyHrs.length; i++) {
System.out.print("Employee" + i + ":" + weeklyHrs[i]);
System.out.println();
}
System.out.println();
int[][] wHTwoD = new int[8][2];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 2; j++) {
if (j % 2 == 0) {
wHTwoD[i][j] = i;
} else {
wHTwoD[i][j] = weeklyHrs[i];
}
}
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(wHTwoD[i][j]);
}
System.out.println();
}
for (int i = 0; i < weeklyHrs.length; i++) {
System.out.print(weeklyHrs[i] + " ");
}
}
public static void sort(int[] arr) {
int temp;
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length; j++) {
if (arr[j] < arr[i]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
}
Question
Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records an employee’s seven-day work hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in increasing order of the total hours.
I got the total weekly hours of each employees in a 1d array like this
int[] weeklyHrs = new int[hours.length];
for (int i = 0; i < hours.length; i++) {
for (int j = 0; j < hours[i].length; j++) {
weeklyHrs[i] = weeklyHrs[i] + hours[i][j];
}
}
However, now I need to sort it and I need to track the index of unsorted array even after sorting it. So far, the book hasn’t started on how to sort 2d array. So I am not going to use this as I take this as a kind of drill.
My concern is simple: How do I track the index of unsorted array after sorting that array?