user299648
user299648

Reputation: 2789

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.

Upvotes: 1

Views: 411

Answers (1)

Stan Kurilin
Stan Kurilin

Reputation: 15802

Try PriorityQueue. It provides O(log(n)) time for the insertion methods (add(), remove()); constant time for the retrieval methods(size(), peek()).

Upvotes: 9

Related Questions