0

Here my output is:

Employee0: 41 hours 
Employee1: 37 hours 
Employee2: 34 hours 
Employee3: 32 hours 
Employee4: 31 hours 
Employee5: 28 hours 
Employee6: 28 hours 
Employee7: 20 hours 

But I need to store my employee information in an array and get this output:

Employee7: 41 hours 
Employee6: 37 hours 
Employee0: 34 hours 
Employee4: 32 hours 
Employee3: 31 hours 
Employee5: 28 hours 
Employee1: 28 hours 
Employee2: 20 hours 

Here is my code as of now with the top output. I cant seem for see how to store the employee information in an array and print the second output.

  /**Main Method**/
    public static void main(String[] args) 
    {
        /**Employee's weekly hours**/
        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[] total = calculateTotal(hours);
        display(selectionSort(total));
    }

    /**Total Hours**/
    public static int[] calculateTotal(int[][] array) 
    {
        int [] totalHours = new int[8];
        for (int i = 0; i < 8; i++) 
        {  
            int sum = 0;  
            for (int j = 0; j < 7; j++) 
            {  
                sum += array[i][j];  
                totalHours[i] = sum;
            }
        }
        return totalHours;
    }

    /**Selection Sort**/
    public static int[] selectionSort(int[] list) 
    {

        for (int i = 0; i < list.length-1; i++) 
        {

            int currentMax = list[i];
            int currentMaxIndex = i;
            for (int j = i + 1; j < list.length; j++) 
            {
                if (currentMax < list[j]) 
                {
                    currentMax = list[j];
                    currentMaxIndex = j;
                }
            }

            if (currentMaxIndex != i) 
            {
                list[currentMaxIndex] = list[i];
                list[i] = currentMax;
            }
        }
        return list;
    }

    /**Display**/
    public static void display(int[] list)
    {
        for (int i = 0; i < list.length; i++)
            System.out.print("Employee" + i + ": " + list[i] + " hours \n");

    }

}
md05062
  • 3
  • 2
  • 6

4 Answers4

1

You are never mapping the employee to the number of hours they are working.

You could consider returning int[][] in calculateTotal. It'd be of the form [[employeeNumber, numberOfHours]]. The result for calculateTotal in your example would look like

[
 [0,34],
 [1,28],
 [2,20],
 [3,31],
 [4,32],
 [5,28],
 [6,37],
 [7,41]
] 

Your selection sort method would then have to sort by index 1 of each nested int[]. You would also need to rewrite your display method to account for the data structure change

Linus Liang
  • 287
  • 2
  • 15
  • Im not sure how i would go about writing that. I can see how it would work, im just cant see how to write it in that way – md05062 Apr 17 '16 at 01:11
  • While I sort the method can I just keep track of the array index in another array that corresponds to the total work hours and print that? – md05062 Apr 17 '16 at 01:15
  • You can certainly do that for this case. There might be an issue if you have two employees with the same work hours. How would you distinguish between the two employees? – Linus Liang Apr 17 '16 at 01:16
  • How would I go about getting the returned array from calculateTotal – md05062 Apr 17 '16 at 01:32
  • Your totalHours array would be `int[][] totalHours = new int[8][7];` Then change `totalHours[i] = sum;` to `totalHours[i][0] = i; totalHours[i][1] = sum;` finally just return `totalHours` at the end of `calculateTotal` – Linus Liang Apr 17 '16 at 01:35
  • You'll also need to change the type of total inside your main method to int[][] – Linus Liang Apr 17 '16 at 01:39
  • Okay then I would sort by the 1 column and print out. Thanks for the help! – md05062 Apr 17 '16 at 01:41
  • Exactly, and np! Let me know if you have any other questions – Linus Liang Apr 17 '16 at 01:46
  • Also, please mark this as the correct answer if you end up getting the right result – Linus Liang Apr 17 '16 at 01:53
1

So, first that won't work because you lose the original index when you sort the array.

The best, most OO approach would be to use a class that stores the number of hours and an identification of the employee.

Here is the solution with an TreeSet:

TreeSet<Employee> employees = new TreeSet<Employee>();

employees.add(new Employee(0,34));

employees.add(new Employee(1,28));
employees.add(new Employee(2,20));
employees.add(new Employee(3,31));
employees.add(new Employee(4,32));
employees.add(new Employee(5,28));
employees.add(new Employee(6,37));
employees.add(new Employee(7,41));

for (Employee employee : employees.descendingSet()) {
    System.out.print("Employee" + employee.getId() + ": " + employee.getHours() + " hours \n");
}

And the Employee class:

public class Employee implements Comparable<Employee>
{
  private Integer id;
  private Integer hours;
  public Employee(int id, int hours)
  {
    this.id = id;
    this.hours = hours;
  }
  public int getId()
  {
    return id;
  }
  public int getHours()
  {
    return hours;
  }
  @Override
  public int compareTo(Employee o) {
    int hoursComparison = hours.compareTo(o.getHours());
    return hoursComparison == 0 ? id.compareTo(o.getId()) : hoursComparison;
  }
}
pca
  • 442
  • 5
  • 18
  • I've upvoted yours, but realized after running the code myself that it doesn't work as expected. the indices of the array gets updated every time you loop through, so it won't work because you have to print out the original indices. – viviboox3 Apr 17 '16 at 02:02
  • You are completely right. I don't know what I was thinking. In any case, I did find a way to make it work, but the complexity is O^2. So it's no really a good solution. This leaves us with the Employee class. Using a TreeSet will make it much simpler. I'll edit my answer to include it. – pca Apr 17 '16 at 09:54
0

I made some fixes to your code. The problem was that you lost the initial index of employee.

    /**Main Method**/
    public static void main(String[] args)
    {
        /**Employee's weekly hours**/
        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}};

        Employee[] total = employees(hours);
        display(selectionSort(total));
    }

    static class Employee {
        private String name;
        private int total;

        public Employee(String name, int total) {
            this.name = name;
            this.total = total;
        }

        public int getTotal() {
            return total;
        }

        public String getName() {
            return name;
        }
    }

    /**Total Hours**/
    public static Employee[] employees(int[][] array)
    {
        Employee [] totalHours = new Employee[8];
        for (int i = 0; i < 8; i++)
        {
            int sum = 0;
            for (int j = 0; j < 7; j++)
            {
                sum += array[i][j];
            }
            totalHours[i] = new Employee("Employee" + i, sum);
        }
        return totalHours;
    }

    /**Selection Sort**/
    public static Employee[] selectionSort(Employee[] list)
    {
        for (int i = 0; i < list.length-1; i++)
        {

            int currentMax = list[i].getTotal();
            int currentMaxIndex = i;
            for (int j = i + 1; j < list.length; j++)
            {
                if (currentMax < list[j].getTotal())
                {
                    currentMax = list[j].getTotal();
                    currentMaxIndex = j;
                }
            }

            if (currentMaxIndex != i)
            {
                final Employee employee = list[currentMaxIndex];
                list[currentMaxIndex] = list[i];
                list[i] = employee;
            }
        }
        return list;
    }

    /**Display**/
    public static void display(Employee[] list)
    {
        for (int i = 0; i < list.length; i++) {
            final Employee employee = list[i];
            System.out.print(employee.getName() + ": " + employee.getTotal() + " hours \n");
        }
    }
Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
0

That's because when you sort the total hours, you lost the index (employee number) of each total hours.

Using a class is easiest way. But consider you may want to use static method only, here is a non-class, but not effcient way.

    /**Main Method **/
public static void main(String[] args) 
{
    /**Employee's weekly hours**/
    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[] total = calculateTotal(hours);
    display(selectionSort(total), total);
}

/**Total Hours**/
public static int[] calculateTotal(int[][] array) 
{
    int [] totalHours = new int[8];
    for (int i = 0; i < 8; i++) 
    {  
        int sum = 0;  
        for (int j = 0; j < 7; j++) 
        {  
            sum += array[i][j];  
            totalHours[i] = sum;
        }
    }
    return totalHours;
}

/**Selection Sort**/
public static int[] selectionSort(int[] list) 
{

    for (int i = 0; i < list.length-1; i++) 
    {

        int currentMax = list[i];
        int currentMaxIndex = i;
        for (int j = i + 1; j < list.length; j++) 
        {
            if (currentMax < list[j]) 
            {
                currentMax = list[j];
                currentMaxIndex = j;
            }
        }

        if (currentMaxIndex != i) 
        {
            list[currentMaxIndex] = list[i];
            list[i] = currentMax;
        }
    }
    return list;
}

/**Display**/
public static void display(int[] list, int[] originList)
{
    for (int i = 0; i < list.length; i++)
    {
        int index = i;
        for (int j = 0; j < list.length; j++)
        {
           if (list[i] == originList[j])
              index = j;
        }

        System.out.print("Employee" + index + ": " + list[i] + " hours \n");

   }

}
Ethan F.
  • 251
  • 1
  • 7