开发者

Java array sorting in reverse! Need to reverse the reverse

开发者 https://www.devze.com 2023-01-27 07:10 出处:网络
Right now I have my array sorting (which is better than getting an error) except it is sorting in the reverse than what I want it to sort in.

Right now I have my array sorting (which is better than getting an error) except it is sorting in the reverse than what I want it to sort in.

   public static void sortDatabase(int numRecords, String[] sDeptArr, 
              int[] iCourseNumArr, int[] iEnrollmentArr)
   {
       System.out.println("\nSort the database. \n");
       String sTemp = null;
       int iTemp = 0;
       int eTemp = 0;
       String a, b = null;
       for(int i=0; i<numRecords; i++)
       {
           int iPosMin = i+1;
           for(int j=iPosMin; j<numRecords; j++)
           {
               a = sDeptArr[i];
               b = sDeptArr[iPosMin];
               if(a.compareTo(b) > 0)
               {
                   sTemp= sDeptArr[j];
                   sDeptArr[j] = sDeptArr[iPosMin];
                   sDeptArr[iPosMin] = sTemp;
                   iTemp = iCourseNumArr[j];
                   iCourseNumArr[j] = iCourseNumArr[iPosMin];
                   iCourseNumArr[iPosMin] = iTemp;
                   eTemp = iEnrollmentArr[j];
                   iEnrollmentArr[j] = iEnrollmentArr[iPosMin];
                   iEnrollmentArr[iPosMin] = eTemp;
               }
               else if(sDeptArr[j].equals(sDeptArr[iPosMin]) && !(iCourseNumArr[j] < iCourseNumArr[iPosMin]))
               {
                   sTemp= sDeptArr[i];
                   sDeptArr[i] = sDeptArr[iPosMin];
                   sDeptArr[iPosMin] = sTemp;
                   iTemp = iCourseNumArr[i];
                   iCourseNumArr[i] = iCourseNumArr[iPosMin];
                   iCourseNumArr[iPosMin] = iTemp;
                   eTemp = iEnrollmentArr[i];
                   iEnrollmentArr[i] = iEnrollmentArr[iPosMin];
    开发者_开发技巧               iEnrollmentArr[iPosMin] = eTemp;
               }
               else continue;
           }

       }
   }

Again, no array lists or array.sorts. I need just to reverse how this is sorting but I have no idea how.


just do a.compareTo(b) < 0 instead of the > 0

EDIT: I've figured out the problem. But since this is homework (thanks for being honest), I won't post my solution, but here are a few tips:

  • You are doing selection sort. The algorithm isn't as complicated as you made it. You only have to swap if the two elements you are checking are in the wrong order. I see you have 3 branches there, no need.

  • Take a look at when you are assigning a and b. Through the inner loop, where j is changing, a and b never change, because i and iPosMin stay the same. I hope that helps.

  • It's always good to break your algorithm down to discreet parts that you know works by extracting methods. You repeat the same swap code twice, but with different arguments for indices. Take that out and just make a:

-

// swaps the object at position i with position j in all arrays
private static void swap(String[] sDeptArr, int[] iCourseNumArr, int[] iEnrollmentArr, int i, int j)

Then you'll see you're code get a lot cleaner.


First I'd say you need to build a data structure to encapsulate the information in your program. So let's call it Course.

public class Course {
   public String department;
   public Integer courseNumber;
   public Integer enrollment;
}

Why not use the built in sort capabilities of Java?

List<Course> someArray = new ArrayList<Course>();
...
Collections.sort( someArray, new Comparator<Course>() {
    public int compare( Course c1, Course c2 ) {
       int r = c1.compareTo( c2 );
       if( r == 0 ) { /* the strings are the same sort by something else */
          /* using Integer instead of int allows us 
           * to compare the two numbers as objects since Integer implement Comparable
           */
          r = c1.courseNumber.compareTo( c2.courseNumber );
       }
       return r;
    }
});

Hope that gets you an A on your homework. Oh and ditch the static Jr. Maybe one day your prof can go over why statics are poor form.


Hmm... I wonder what would happen of you altered the line that reads if(a.compareTo(b) > 0)?

0

精彩评论

暂无评论...
验证码 换一张
取 消