简体   繁体   English

将数组从高到低排序

[英]Sort an array From highest to lowest

I am trying to sort an array in Java, I want top 3 value only.我正在尝试用 Java 对数组进行排序,我只想要前 3 个值。

// A:55,B:45,C:5,D:35,E:35,F:1 // A:55,B:45,C:5,D:35,E:35,F:1

       int a[] = {A,B,C,D,E,F};

       float first =0;
       float second=0;
       float third =0;

       for (int i=0; i<a.length; i++){
           if(first <= a[i]){
               first=a[i];
           }
       }System.out.println("first largest is "+first);

       for (int j=0; j<a.length; j++){
           if(a[j] <=first && a[j] > second){
               second = a[j];
           }
       }System.out.println("second largest is "+second);

       for (int k=0;k<a.length; k++){
           if(a[k]<=second && a[k]>third){
                       
               third =a[k];
           }
       }System.out.println("third largest is "+third);

Another thing here is that if A and B are equal, it should give me A and if D and E are equal it should provide D as answer.这里的另一件事是,如果 A 和 B 相等,它应该给我 A,如果 D 和 E 相等,它应该提供 D 作为答案。 So final answer should be ABD.所以最终答案应该是ABD。

you can store in the variable first,second,third the index instead of value您可以在变量first,second,third中存储索引而不是值

I rewrite the code quickly it not optimal but I think it can help我很快重写了代码,它不是最佳的,但我认为它可以提供帮助

    public static void main(String[] args) {

    char names[] = {'A', 'B', 'C', 'D', 'E', 'F'};
    int values[] = {55, 45, 5, 35, 35, 1, Integer.MIN_VALUE};

    int first = values.length - 1;
    int second = values.length - 1;
    int third = values.length - 1;

    for (int i = 0; i < values.length; i++) {
        if (values[first] < values[i]) {
            first = i;
        }
    }
    System.out.println("first largest is " + names[first]);

    for (int j = 0; j < values.length; j++) {
        if (j != first && values[second] < values[j]) {
            second = j;
        }
    }
    System.out.println("second largest is " + names[second]);

    for (int k = 0; k < values.length; k++) {
        if (k != first && k != second && values[third] < values[k]) {

            third = k;
        }
    }
    System.out.println("third largest is " + names[third]);

}

I added the Integer.MIN_VALUE to the end of list to be as the initial index for the variable first ,second and third you cad replace it with 0 if you need but it will never work with negative number then我加入了Integer.MIN_VALUE的,以列表的末尾是作为变量的初始指数第一,第二第三你CAD 0替换它,如果你需要,但它永远不会与负数的工作,然后

Max Heap data structure is your friend: Max Heap数据结构是你的朋友:

public static void main(String[] args) {
    char[] arr = { 'A', 'B', 'C', 'D', 'E', 'F' };

    List<Character> sorted = sortAngGetTopThree(arr);

    System.out.println("first largest is " + sorted.get(0));
    System.out.println("second largest is " + sorted.get(1));
    System.out.println("third largest is " + sorted.get(2));
}

public static List<Character> sortAngGetTopThree(char... arr) {
    Map<Character, Integer> map = Map.of('A', 55, 'B', 45, 'C', 5, 'D', 35, 'E', 35, 'F', 1);

    Comparator<Character> sortByValueAndLetterDesc = (one, two) -> {
        int res = Integer.compare(map.get(two), map.get(one));  // sort by number desc first
        return res == 0 ? Character.compare(one, two) : res;    // sort by letter asc second
    };

    Queue<Character> maxHeap = new PriorityQueue<>(sortByValueAndLetterDesc);

    for (char ch : arr)
        maxHeap.add(ch);

    List<Character> res = new ArrayList<>(3);

    for (int i = 0; i < 3 && !maxHeap.isEmpty(); i++)
        res.add(maxHeap.remove());

    return res;
}

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

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