I have two different lists that contain integers and I need to constantly find the two smallest values between these two lists; I should note that I do NOT want to merge these two lists together, since they are different types.
I would like to know if my approach is good or bad. If it is bad, please let me know how I can make it more efficient.
Finding a min in
List can be done in linear time without any sorting. Sorting and finding the min every time will be
O(m*nlgn) m being the number of iterations and n the size of the list) .
A better way would to use
PriorityQueue (min-heap) where the min is always on the top of the heap instead of sorting on every iteration.
min-heap is common in implementing
Huffman codes and greedy algorithms in general.