What are the most common data structures and the Big O for operations on them?
I am trying to get a grasp on Big O notations. It seems pretty abstract. I selected the most common data structures - array, hash, linkedl list (single and double) and a binary search tree and guessed somewhat at the Big O notation for 开发者_开发百科the most common operatons - insert and search. This is preparation for an inerview. I need to learn just the basics not read a whole text book on algorithms though this would be ideal. Is the table below valid?
Data Structure Big O Search Big O Insert
Array O(1) O(n)
Hash O(1) O(1)
Single Linked List O(n) O(1)
Double Linked List O(n) O(1)
Tree O(log n) O(log n)
For Array, to get/return an element takes O(1), but to search for an element should take O(n). For Tree, I assume that you meant balanced binary search tree.
For hash inserts, remember that O(1) is optimal. If your hash table is close to full, your efficiency will approach O(n).
Also, for a sorted array, searching is O(log n).
精彩评论