If searching a sorted List is O(log2 n) and searching a balanced BST is also O(log2 n), which one should I use assuming the following:
- Elements will be received unsorted and then sorted after all the elements are loaded
- No elements will be removed
Which should be used and why (Sorted List or Binary Search Tree)?
Thanks.
If you sort the data once and then only search, then the asymptotic complexities of both approaches will be the same.
I think sorted list will have much lower actual running time and memory consumption, indexing into an array is extremely fast.
That being said, maybe using a hash table would be better for you. Creating it is O(n) (compared with O(n log n) of the previous approaches) and retrieving one item is O(1) (compared with O(log n)).