Susanna Michael
Susanna Michael

Reputation: 161

Data structure suited to store number of integers in sorted order

This is a question related to data structures.

Your system measures temperature every second. The input data is an integer. At any point in time i should be able to retrieve the highest 5 temperature records. What is the best data structure to store this data.

Upvotes: 0

Views: 714

Answers (1)

Mad Physicist
Mad Physicist

Reputation: 114320

A heap, or equivalently, a sorted array would work.

Checking if your new time needs to be inserted at all is O(1). Checking the location is O(log n) (where n is 5). Depending on the optimization, with five elements, the move and insert could be O(n) or O(1).

Upvotes: 2

Related Questions