排序一个数组,从最高到最低。

huangapple go评论73阅读模式
英文:

Sort an array From highest to lowest

问题

我正在尝试在Java中对数组进行排序,我只想要前3个最大的值。

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

另一件事是,如果A和B相等,它应该给我A,如果D和E相等,它应该给我D作为答案。因此,最终答案应该是ABD。

英文:

I am trying to sort an array in Java, I want top 3 value only.

// 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&lt;a.length; i++){
	       if(first &lt;= a[i]){
	           first=a[i];
	       }
	   }System.out.println(&quot;first largest is &quot;+first);

	   for (int j=0; j&lt;a.length; j++){
	       if(a[j] &lt;=first &amp;&amp; a[j] &gt; second){
	           second = a[j];
	       }
	   }System.out.println(&quot;second largest is &quot;+second);

	   for (int k=0;k&lt;a.length; k++){
	       if(a[k]&lt;=second &amp;&amp; a[k]&gt;third){
	    	      	   
	           third =a[k];
	       }
	   }System.out.println(&quot;third largest is &quot;+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. So final answer should be ABD.

答案1

得分: 1

你可以将索引存储在变量first、second、third中,而不是存储值。

我快速地重写了代码,虽然不是最优的,但我认为它可以帮助。

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]);

}

我在列表末尾添加了Integer.MIN_VALUE,以将其作为变量first、secondthird的初始索引,如果需要,你可以将其替换为0,但它将不适用于负数。

英文:

you can store in the variable first,second,third the index instead of value

I rewrite the code quickly it not optimal but I think it can help

    public static void main(String[] args) {

    char names[] = {&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;};
    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 &lt; values.length; i++) {
        if (values[first] &lt; values[i]) {
            first = i;
        }
    }
    System.out.println(&quot;first largest is &quot; + names[first]);

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

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

            third = k;
        }
    }
    System.out.println(&quot;third largest is &quot; + 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

答案2

得分: 1

[最大堆][1] 数据结构是你的朋友

    public static void main(String[] args) {
        char[] arr = { 'A', 'B', 'C', 'D', 'E', 'F' };
    
        List<Character> sorted = sortAndGetMethodTopThree(arr);
    
        System.out.println("第一个最大的是 " + sorted.get(0));
        System.out.println("第二个最大的是 " + sorted.get(1));
        System.out.println("第三个最大的是 " + sorted.get(2));
    }
    
    public static List<Character> sortAndGetMethodTopThree(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));  // 首先按数字降序排序
            return res == 0 ? Character.compare(one, two) : res;    // 其次按字母升序排序
        };
    
        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;
    }

  [1]: https://en.wikipedia.org/wiki/Min-max_heap
英文:

Max Heap data structure is your friend:

public static void main(String[] args) {
char[] arr = { &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39; };
List&lt;Character&gt; sorted = sortAngGetTopThree(arr);
System.out.println(&quot;first largest is &quot; + sorted.get(0));
System.out.println(&quot;second largest is &quot; + sorted.get(1));
System.out.println(&quot;third largest is &quot; + sorted.get(2));
}
public static List&lt;Character&gt; sortAngGetTopThree(char... arr) {
Map&lt;Character, Integer&gt; map = Map.of(&#39;A&#39;, 55, &#39;B&#39;, 45, &#39;C&#39;, 5, &#39;D&#39;, 35, &#39;E&#39;, 35, &#39;F&#39;, 1);
Comparator&lt;Character&gt; sortByValueAndLetterDesc = (one, two) -&gt; {
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&lt;Character&gt; maxHeap = new PriorityQueue&lt;&gt;(sortByValueAndLetterDesc);
for (char ch : arr)
maxHeap.add(ch);
List&lt;Character&gt; res = new ArrayList&lt;&gt;(3);
for (int i = 0; i &lt; 3 &amp;&amp; !maxHeap.isEmpty(); i++)
res.add(maxHeap.remove());
return res;
}

huangapple
  • 本文由 发表于 2020年9月7日 02:22:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63767514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定