Java数组逆序排序!需要反向反转

发布于 2024-10-05 02:04:27 字数 1829 浏览 4 评论 0原文

现在我已经进行了数组排序(这比收到错误更好),只是它的排序与我希望的排序相反。

   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;
           }

       }
   }

同样,没有数组列表或 array.sorts。我只需要反转排序方式,但我不知道如何排序。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沧笙踏歌 2024-10-12 02:04:27

只需执行 a.compareTo(b) a.compareTo(b) a.compareTo(b) < 0 而不是 >; 0

编辑:我已经解决了问题。但由于这是家庭作业(感谢诚实),我不会发布我的解决方案,但这里有一些提示:

  • 您正在进行选择排序。该算法并不像您编写的那么复杂。仅当您正在检查的两个元素顺序错误时才需要交换。我看到你那里有 3 个分支,不需要。

  • 看看您何时分配 ab。通过内部循环,其中 j 发生变化,ab 永远不会改变,因为 iiPosMin 保持不变。我希望这会有所帮助。

  • 通过提取方法将算法分解为您知道可以工作的离散部分总是好的。您重复相同的交换代码两次,但使用不同的索引参数。把它拿出来,然后做一个:

-

// 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)

然后你会发现你的代码变得更加干净。

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.

潜移默化 2024-10-12 02:04:27

首先,我想说您需要构建一个数据结构来封装程序中的信息。所以我们称之为课程。

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

为什么不使用 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;
    }
});

希望你的作业能得到 A。哦,放弃静态的 Jr。也许有一天你的教授可以讨论为什么静态是糟糕的形式。

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.

贱贱哒 2024-10-12 02:04:27

嗯...我想知道如果您更改了 if(a.compareTo(b) > 0) 行,会发生什么?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文