-
Simple Sorting Problem, Please Help!
I am new to the website with a small problem. I know it is probably really simple and I heard you guys could help me.
My objective is to:Computing weekly hours for each employee. Suppose the weekly hours for all employees are stored in a two-dimensional array;
Each row records an employee's 7 day work hours with seven
columns. I need to write a program that displays employee numbers (0-7) and their work hours in DECREASING order of work hours (use the sort of your choice. Store the total work hours into a second one-dimensional array, and sort the second array. You could use a third array storing "employee #" and swap it when you swap the array containing total hours worked.
So far my code is:
public class HW5b
{
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}};
for(int i = 0; i < hours.length; i++)
{
int totalHours = 0;
for(int n = 0; n < hours[i].length; n++)
{
totalHours += hours[i][n];
}
swapEmployeeMethod(hours, totalHours);
System.out.println("Employee " + i + "'s hours are " + totalHours);
}
}
public static void swapEmployeeMethod(int[][] hours, int totalHours)
{
//I need to figure out how to swap the suckers here
}
}
I am sure this is a piece of cake for one of you and I would appreciate some pointers as a newbie to Java. Thanks!
-
Sorting Problem
It depends on the data structure you choose to use to store the combination of employee number and total hours worked. Could be as easy as another 2D array, 7 x 2, holding the data ... each row would be a pair, and you can sort (rows) based upon the value of total hours worked.
You can also use other structures, especially those which allow you to treat paired values as a single unit ...
-
Here is a bubblesort algorithm.
This one sorts both ways.
Code:
/**
* Bubblesort algorithm
* @author sjalle
* @version 1.0
*/
public class BubbleSort {
public final static int ASCENDING=0;
public final static int DESCENDING=1;
private int [] values;
private int sortDirection=ASCENDING;
public BubbleSort(int [] values, int sortDirection) {
this.values=values;
this.sortDirection=sortDirection;
}
public void sort() {
boolean wasSwapped;
for (int i=values.length-1;i>=0; i--) {
wasSwapped=false;
for (int j=0;j<i;j++) {
if (swap(j)) wasSwapped=true;
}
/**
* if a scan completes without any swapping the array is sorted
*/
if (!wasSwapped) break;
}
}
private boolean swap(int pos) {
if ( (this.sortDirection==ASCENDING && values[pos] > values[pos+1]) ||
(this.sortDirection==DESCENDING && values[pos] < values[pos+1])) {
int copy=values[pos];
values[pos]=values[pos+1];
values[pos+1]=copy;
return true;
} else {
return false;
}
}
public static void main(String[] args) {
int [] v=new int[] {
2,4,3,5,4,6,7,5,3,1,33,44,22
};
BubbleSort bs=null;
System.out.println("Unsorted:");
listValues(v);
bs = new BubbleSort(v, BubbleSort.DESCENDING);
bs.sort();
System.out.println("Sorted Decending:");
listValues(v);
bs = new BubbleSort(v, BubbleSort.ASCENDING);
bs.sort();
System.out.println("Sorted Ascending:");
listValues(v);
}
static void listValues(int [] v) {
for (int i=0;i<v.length;i++) {
System.out.println(v[i]);
}
}
}
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks