简体   繁体   English

如何在java中使用二分搜索找到排名2的学生?

[英]How to find the rank 2 student with binary search at java?

In an exam that was attended by 400 students, the teacher wanted to give a special gift to the student who was ranked 2. If all participants are identified by a test number (1-400) create the MOST EFFICIENT algorithm and program to find students who will be rewarded!在有 400 名学生参加的考试中,老师想给排名第 2 的学生一份特别的礼物。如果所有参与者都通过测试编号 (1-400) 进行识别,请创建最有效的算法和程序来寻找学生谁会得到奖励!

*) The same value has the same rank (for example 100 100 98 98 then there are 2 people in the 1st rank and 2 people in the 2nd rank) *) 相同的值具有相同的等级(例如 100 100 98 98 那么第一等级有 2 个人,第二等级有 2 个人)

note: Help me, how is the code in java, i really confuse :(注意:帮帮我,java中的代码如何,我真的很困惑:(

You don't need to sort the input, simply:您不需要对输入进行排序,只需:

  1. store current top rank, and the list of people in it (initially null/empty)存储当前的最高排名,以及其中的人员列表(最初为空/空)
  2. store current second top rank, and the list of people in it (initially null/empty)存储当前的第二个最高排名,以及其中的人员列表(最初为空/空)

loop through the items and:遍历项目并:

  1. if current rank > top rank => second top rank is replaced by current top rank, and current top rank is replaced by (rank/the current item)如果当前排名 > 最高排名 => 第二最高排名被当前最高排名替换,并且当前最高排名被替换为(排名/当前项目)
  2. if current rank = top rank => push student onto current top rank list如果当前排名 = 最高排名 => 将学生推入当前最高排名列表
  3. if current rank < top rank => do checks 1 and 2, but this time for second rank如果当前排名 < 最高排名 => 检查 1 和 2,但这次是第二排名

For instance, assume the items are Joe:1 Jack:2 Jill:3, Jim: 1, Job: 2 then:例如,假设项目是 Joe:1 Jack:2 Jill:3, Jim: 1, Job: 2 然后:

Joe ranks higher than null => current top rank is 1 and items are [Joe] Joe 排名高于 null => 当前最高排名为 1,项目为 [Joe]

Jack ranks higher than current top (1) => set top rank to 2 and items to [Jack], and also set second top to previous top, ie 1 and [Joe] Jack 排名高于当前 top (1) => 将 top rank 设置为 2 并且将 items 设置为 [Jack],并将第二个 top 设置为上一个 top,即 1 和 [Joe]

Jill ranks higher than current top (2) => set top rank to 3 and items to [Jill], and also set second top to previous top, ie 2 and [Jack] Jill 排名高于当前 top (2) => 将 top rank 设置为 3,项目为 [Jill],并将第二个 top 设置为上一个 top,即 2 和 [Jack]

Jim ranks lower than current top (3) and lower than second top (2) => nothing to do Jim 的排名低于当前的顶级 (3) 并且低于第二名 (2) => 无事可做

Job ranks lower than current top (3) but is equal to second top (2) => append Job to second top list, so it is [Jack and Job]工作排名低于当前的顶部 (3) 但等于第二个顶部 (2) => 将工作附加到第二个顶部列表,所以它是 [Jack and Job]

The result is top: 3 and [Jill] and second top: 2 and [Jack and Job].结果是 top: 3 和 [Jill] 和第二个 top: 2 和 [Jack and Job]。

If the input is sorted, you can use a binary search, but sorting is more expensive (n log n) than just doing the above.如果输入已排序,您可以使用二分搜索,但排序比仅执行上述操作更昂贵 (n log n)。

You must have two arrays : one for the identifying numbers of students and the other with their score Lets name : "a" the score array and "b" the identifying numbers array你必须有两个数组:一个是学生的识别号码,另一个是他们的分数让我们命名:“a”分数数组和“b”识别数字数组

This will have you arrays sorted:这将使您对数组进行排序:

public static void bubbleSort(int[] a,int[]b) {
        boolean sorted = false;
        int temp;
        while(!sorted) {
            sorted = true;
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] > a[i+1]) {
                    temp = a[i];
                    a[i] = a[i+1];
                    a[i+1] = temp;
                    temp = b[i];
                    b[i] = b[i+1];
                    b[i+1] = temp;
                    sorted = false;
                }
            }
        }
    }

after you short them find the second biggest number with this在你做空他们之后,用这个找到第二大的数字

   static int second(int []a){
        int number =a[0];//case all of them had same score
        for(int i=0;i<a.length-1;i++){
            if (a[i]>a[i+1]){
                number= a[i+1];
                break;
            }
        }
        return number;
    }

and now store the result in a variable number and:现在的结果存储在一个变量number和:

for(int i=0;i<a.length;i++){
            if (a[i]==number){
                System.out.println(b[i]);
            }
        }

Of course there are more solutions but this is mine...当然还有更多的解决方案,但这是我的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM