Simple Sorting Problem, Please Help!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Simple Sorting Problem, Please Help!

  1. #1
    Join Date
    Feb 2005
    Posts
    1

    Exclamation 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!

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468

    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 ...

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center