What is the best data structure and algorithm in this situation?
I have a program that is just one big for loop. I first have an empty set. On each iteration of the for loop I need to peek and remove the minimum value from the set. Also in each iteration I can add anywhere from 0 to 8 values to the set(values are random). Which built in Java data structure should I use? I considered doing a bubble sort with an ArrayList and just taking out the first index. I'm am looking for the the fastest algorithm to accomplish this task. 开发者_StackOverflow中文版
Try PriorityQueue. It provides O(log(n)) time for the insertion methods (add()
, remove()
); constant time for the retrieval methods(size()
, peek()
).
精彩评论