In designing algorithms, we will choose the data structure that most fulfil our needs.
Which data structure is the best when using it for both searching and insertion/deletion?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Arrays
Searching
If it is unsorted, we need to iterate through the whole array
If sorted, we can use binary search
Insertion
If it is unsorted, we can easily append it to the back of our array
If it is sorted, that means we need to insert it in the right place by iterating through the whole array to find the suitable place for it. The worst case for it is when it should be inserted at the back of the array, so it is not really useful when doing insertion.
Deletion
If it is unsorted, we can change the value of the item we want to delete with the last item of the array then just remove the last element of the array, which will take constant time.
If it is sorted, we need to move all elements that are behind the element which we want to delete to replace the blank space after removing the element's value.
Linked List
Searching
Insertion/Deletion
Binary Search Tree
Searching
Insertion
Deletion
Therefore, Binary Search Tree is the best when designing an algorithm for searching and insertion/deletion.