If you have SortedDictionary
or SortedList
, you can use .First()
(or dict.Keys[0]
for SortedList
). Otherwise, you can do:
dict[dict.Keys.Min()]
which would have a total time of O (N) (since Min () should iterate the whole collection)
.First()
will probably have O (1) time for SortedList and O (log n) for SortedDictionary.
Insertion and deletion will be O (log N) time for SortedDictionary and may be up to O (N) for SortedList. Please note: if you use a dictionary to support your priority queue, you cannot have two elements with the same priority.
I don’t think the class has a special implementation for Last, so if you need a key with the highest value, you should probably use a SortedList, since you can do dict.Keys[dict.Count-1]
. If you only need the highest (and not the lowest), you can use Comparer to sort in that order and use First.
Random832
source share