**Runtime Complexity Comparison** | Data Structure | Find | Insert | Delete given position | Find + delete at position| Access by index | Space | |-------------------------------------|----------|----------|-----------------------|--------------------------|-----------------|-------| | **Unsorted Array order important** | O(n) | O(1)* | O(n) | O(n) | O(1) | O(n) | | **Unsorted Array - just raw data ** | O(n) | O(1)* | O(1)** | O(n) | O(1) | O(n) | | **Sorted Array** | O(log n) | O(n) | O(n) | O(n) | O(1) | O(n) | | **Unsorted Linked List** | O(n) | O(1)† | O(1) | O(n) | O(n) | O(n) | | **Sorted Linked List** | O(n) | O(n) | O(1) | O(n) | O(n) | O(n) | | **Unbalanced BST** | O(n) | O(n) | O(1) | O(n) | O( log n)*** | O(n) | | **Balanced BST** | O(log n) | O(log n) | O(log n)†† | O(log n) | O( log n)*** | O(n) | | **HashMap** | O(1)‡ | O(1)‡ | O(1)‡ | O(1)‡ | N/A | O(M)‡‡ | \* Insert at end of array. Insert at arbitrary position is O(n) due to shifting elements. ** Delete and swap last position with just deleted. *** Extra credit on AVL assignment from cs280 - hint, keep track of the number of nodes in each subtree **†** Insert in front of the linked list. Insert at arbitrary position requires O(n) to find the position first. **††** Cost of rebalancing the tree after deletion. **‡** Average case complexity. Worst case is O(n) due to hash collisions. **‡‡** Where M is the size of the hash table, which will be larger than the number of elements stored. # Additional Considerations ## Arrays - **Sorted Array Search**: Uses binary search for O(log n) complexity - **Memory**: Contiguous memory allocation provides good cache locality - **Resizing**: Dynamic arrays may require O(n) reallocation when capacity is exceeded ## Linked Lists - **No Random Access**: Must traverse from head to reach specific positions - **Memory Overhead**: Extra memory required for storing pointers - **Cache Performance**: Poor cache locality due to non-contiguous memory allocation ## Binary Search Trees - **Unbalanced BST**: Can degrade to linked list performance in worst case (e.g., inserting sorted data) - **Balanced BST**: Examples include AVL trees, Red-Black trees, which maintain balance through rotations - **In-order Traversal**: Provides sorted output in O(n) time ## HashMap - **Load Factor**: Performance depends on maintaining appropriate load factor - **Hash Function**: Quality of hash function affects collision frequency - **Collision Resolution**: Common methods include chaining and open addressing