How to find the 100th largest number from an array

anand

I have to find the 90-100th largest number from an array when an array is expanding and the data are being populated or added every seconds or less than a seconds. May be considered as a stream of running data. I thought of using the data structure Binary tree but every time constructing the tree and placing the new data at the appropriate place will be quite tedious since data's are coming very fast.

I want to know which data structure will be most suitable to find the 90-100 th largest data.

Jim Mischel

One solution is to use a Min heap with size 100. As each item comes in, you compare it to the smallest item in the heap. If it's larger than the smallest item in the heap, you remove the smallest item and replace it with the new item. Whenever you want the 100th largest item, you just get the minimum item from the heap.

In pseudocode, it looks something like this:

heap = new MinMaxHeap()
while (item available)
{
    if (heap.count < 100)
    {
        add item to heap
    }
    else if (item > heap.PeekMin())
    {
        remove minimum item from heap
        add item to heap
    }
}

Any time you want the 100th largest item, just do a PeekMin on the heap.

Examining the smallest item (i.e. PeekMin) is an O(1) operation. Removal and insertion are each O(log k), where k is the number of items on the heap. In practice, assuming items are being presented in random order, you'll only have to do remove and replace on about 10% of the items. Worst case, when items are presented in ascending order, you'll have to do remove/replace on every item.

Now, if you want the 90th through 100th items, you'll need to sort the heap and take the first 11 items. Fortunately, a sorted array is a valid binary heap, so sorting won't destroy the heap property. And you're only sorting 100 items, so it's going to be very fast.

Implementing a simple binary heap as an array is very easy. I described it at length in a series of blog posts, starting with Priority Queues. The code examples are in C#, but you could easily convert them to php.

Another way to do this is with two heaps. The first heap is as described above: the top 100 items. The second heap would be a Max heap in which you store the smallest 11 items from the top 100. Your replacement policy on the second heap is the opposite. That is, you compare the new item to the item at the root of the heap and replace if the new item is smaller. With this structure, you'll still have to sort that second heap if you want the items in order, but sorting 11 items will be really fast.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find the 100th largest number from an array

From Dev

how to form the largest number from a set of an array in javaScript

From Dev

How to find largest value from array of NSDictionary objects?

From Dev

How can i find the largest gameobject size from array?

From Dev

How to find the Largest Difference in an Array

From Dev

Find the largest number and its index in the random array

From Dev

Find the largest number in an array of integers javascript

From Dev

Python: finding the +100th prime number

From Dev

How to find largest sequence of a given number in excel?

From Dev

i want the largest number from array in php

From Dev

how to run jupyter notebook from 5th cell to 100th cell, without running other part of the notebook?

From Dev

How to find specific number from array?

From Dev

How to find the 2nd largest number in the array, but return the last index that the value appears in?

From Dev

How to find the largest element of an array of known size?

From Dev

How to find array index of largest value?

From Dev

How do I find the largest number from a list and its occurrences using lambdas?

From Dev

Function to find largest number

From Dev

Find textbox with the largest number

From Dev

Find second largest number

From Dev

How to get the largest number from a list in clojure

From Dev

How to get largest number from Firebase datalist

From Dev

How to get the largest number from a list in clojure

From Dev

How to print largest number from User Input

From Dev

Find the order of numbers which will give the largest number in an array

From Dev

Find the average. largest, smallest number and mode of a int array

From Dev

Find largest number of not sorted array without using linear and binary

From Dev

Find largest and smallest number in array using pointers in C

From Dev

How am I able to display the largest and the smallest number from the array of the user's input?

From Dev

Find the largest number from a string using a regular expression

Related Related

  1. 1

    How to find the 100th largest number from an array

  2. 2

    how to form the largest number from a set of an array in javaScript

  3. 3

    How to find largest value from array of NSDictionary objects?

  4. 4

    How can i find the largest gameobject size from array?

  5. 5

    How to find the Largest Difference in an Array

  6. 6

    Find the largest number and its index in the random array

  7. 7

    Find the largest number in an array of integers javascript

  8. 8

    Python: finding the +100th prime number

  9. 9

    How to find largest sequence of a given number in excel?

  10. 10

    i want the largest number from array in php

  11. 11

    how to run jupyter notebook from 5th cell to 100th cell, without running other part of the notebook?

  12. 12

    How to find specific number from array?

  13. 13

    How to find the 2nd largest number in the array, but return the last index that the value appears in?

  14. 14

    How to find the largest element of an array of known size?

  15. 15

    How to find array index of largest value?

  16. 16

    How do I find the largest number from a list and its occurrences using lambdas?

  17. 17

    Function to find largest number

  18. 18

    Find textbox with the largest number

  19. 19

    Find second largest number

  20. 20

    How to get the largest number from a list in clojure

  21. 21

    How to get largest number from Firebase datalist

  22. 22

    How to get the largest number from a list in clojure

  23. 23

    How to print largest number from User Input

  24. 24

    Find the order of numbers which will give the largest number in an array

  25. 25

    Find the average. largest, smallest number and mode of a int array

  26. 26

    Find largest number of not sorted array without using linear and binary

  27. 27

    Find largest and smallest number in array using pointers in C

  28. 28

    How am I able to display the largest and the smallest number from the array of the user's input?

  29. 29

    Find the largest number from a string using a regular expression

HotTag

Archive