Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the time complexity of indexing, inserting and removing from common data structures?

There is no summary available of the big O notation for operations on the most common data structures including arrays, linked lists, hash tables etc.

like image 531
James Avatar asked Sep 23 '08 18:09

James


People also ask

What is the time complexity for insertion and deletion?

If you don't know the location, then you need to traverse the list to the location of deletion/insertion, which takes O(n) time. ** The deletion cost is O(log n) for the minimum or maximum, O(n) for an arbitrary element.

What is the time complexity of array remove index method?

Deleting an element from an array takes O(n) time even if we are given index of the element to be deleted. The time complexity remains O(n) for sorted arrays as well. In linked list, if we know the pointer to the previous node of the node to be deleted, we can do deletion in O(1) time.


2 Answers

Information on this topic is now available on Wikipedia at: Search data structure

+----------------------+----------+------------+----------+--------------+ |                      |  Insert  |   Delete   |  Search  | Space Usage  | +----------------------+----------+------------+----------+--------------+ | Unsorted array       | O(1)     | O(1)       | O(n)     | O(n)         | | Value-indexed array  | O(1)     | O(1)       | O(1)     | O(n)         | | Sorted array         | O(n)     | O(n)       | O(log n) | O(n)         | | Unsorted linked list | O(1)*    | O(1)*      | O(n)     | O(n)         | | Sorted linked list   | O(n)*    | O(1)*      | O(n)     | O(n)         | | Balanced binary tree | O(log n) | O(log n)   | O(log n) | O(n)         | | Heap                 | O(log n) | O(log n)** | O(n)     | O(n)         | | Hash table           | O(1)     | O(1)       | O(1)     | O(n)         | +----------------------+----------+------------+----------+--------------+   * The cost to add or delete an element into a known location in the list     (i.e. if you have an iterator to the location) is O(1). If you don't     know the location, then you need to traverse the list to the location    of deletion/insertion, which takes O(n) time.   ** The deletion cost is O(log n) for the minimum or maximum, O(n) for an    arbitrary element. 
like image 108
Mobiletainment Avatar answered Nov 05 '22 17:11

Mobiletainment


I guess I will start you off with the time complexity of a linked list:

Indexing---->O(n)
Inserting / Deleting at end---->O(1) or O(n)
Inserting / Deleting in middle--->O(1) with iterator O(n) with out

The time complexity for the Inserting at the end depends if you have the location of the last node, if you do, it would be O(1) other wise you will have to search through the linked list and the time complexity would jump to O(n).

like image 37
Jose Vega Avatar answered Nov 05 '22 18:11

Jose Vega