Finding Nth item of unsorted list without sorting the list

前端 未结 9 2082
耶瑟儿~
耶瑟儿~ 2020-12-09 03:41

Hey. I have a very large array and I want to find the Nth largest value. Trivially I can sort the array and then take the Nth element but I\'m only interested in one element

9条回答
  •  旧时难觅i
    2020-12-09 04:31

    You essentially want to produce a "top-N" list and select the one at the end of that list.

    So you can scan the array once and insert into an empty list when the largeArray item is greater than the last item of your top-N list, then drop the last item.

    After you finish scanning, pick the last item in your top-N list.

    An example for ints and N = 5:

    int[] top5 = new int[5]();
    top5[0] = top5[1] = top5[2] = top5[3] = top5[4] = 0x80000000; // or your min value
    
    for(int i = 0; i < largeArray.length; i++) {
        if(largeArray[i] > top5[4]) {
           // insert into top5:
           top5[4] = largeArray[i];
    
           // resort:
           quickSort(top5);
        }
    }
    

提交回复
热议问题