Originally posted by: koloastreet
like a map vs array vs stack?
if not, anyone know of an easy read somewhere online?
thanks...
oh its for a possible interview coming up this week and its been 5 yrs since i cracked open a CS book. stupid me....
it depends a lot on the underlying implementation of each data structure.
An array is easy, looking up a random element by index is O(1) -- looking at all the elements in order is O(n).
If th array is sorted you can search for an element in O(log n) time using binary search.
Note: "search" and "look up" are different -- think of the meaning.
A linked list, take O(n) time to look up a random element (by index/position). if it is a sorted linked list, it still takes O(n) time to search for an element.
A stack can be implemented wiht a linked list OR an array or in other ways (?).
A stack "needs" to have quick pop/push operations and these are O(1) for both array and linked list based stacks.
Maps/Hashtables are pretty complicated, you need to know a LOT about the underlying implementation before you can answer these questsion.
BUt in general, you should expect to have O(log n) look up a value by key, where n is the size of the map -- not the number of (key, values) in the map. Maps usually have some amount of unused/wasted space in them.
A map could be implemented naively using a linked list, this will give you O(n) performance for just about everything -- so the implementation IS important.