- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Complexity Cheat Sheet for Python Operations
Time complexity is a measure of time, which determines the time required for the algorithm to execute. It also checks how the running time is being increased when the size of the inputs is getting increased. The time complexity is represented with the notation capital O, which sets an upper bound in the worst case performance of an algorithm.
Whenever we design an algorithm we should keep in mind not only about the correctness but also about the time complexity and performance characteristics.
For example, if an algorithm has time complexity O(n) that would take twice of O(n) to execute the given algorithm with double input size and if we take another algorithm with the time complexity O(n^2) then the algorithm takes four times of O(n^2) to execute with the double input size.
List Time Complexity
Internally, the list is considered as an array. The below is the cheat sheet of the list operations with time complexity to execute in the python.
Operation |
Average Case |
Amortized Worst Case |
---|---|---|
Copy |
O(n) |
O(n) |
Append[1] |
O(1) |
O(1) |
Pop last |
O(1) |
O(1) |
Pop intermediate[2] |
O(n) |
O(n) |
Insert |
O(n) |
O(n) |
Get Item |
O(1) |
O(1) |
Set Item |
O(1) |
O(1) |
Delete Item |
O(n) |
O(n) |
Iteration |
O(n) |
O(n) |
Get Slice |
O(k) |
O(k) |
Del Slice |
O(n) |
O(n) |
Set Slice |
O(k+n) |
O(k+n) |
Extend[1] |
O(k) |
O(k) |
Sort |
O(n log n) |
O(n log n) |
Multiply |
O(nk) |
O(nk) |
x in s |
O(n) |
|
min(s), max(s) |
O(n) |
|
Get Length |
O(1) |
O(1) |
Usually, 'n' is the number of elements currently present in the list and 'k' is either the value of a parameter or the number of elements in the parameter.
Collections.deque
The deque is abbreviated as double-ended queue and it is internally represented as a doubly linked list. Following is the cheat sheet for the deque operations along with the time complexity.
Operation |
Average Case |
Amortized Worst Case |
---|---|---|
Copy |
O(n) |
O(n) |
append |
O(1) |
O(1) |
appendleft |
O(1) |
O(1) |
pop |
O(1) |
O(1) |
popleft |
O(1) |
O(1) |
extend |
O(k) |
O(k) |
extendleft |
O(k) |
O(k) |
rotate |
O(k) |
O(k) |
remove |
O(n) |
O(n) |
Get Length |
O(1) |
O(1) |
Set
Following is the cheat sheet of the set data structure operations with the time complexity.
Operation |
Average case |
Worst Case |
---|---|---|
x in s |
O(1) |
O(n) |
Union s|t |
O(len(s)+len(t)) |
|
Intersection s&t |
O(min(len(s), len(t))) |
O(len(s) * len(t)) |
Multiple intersection s1&s2&..&sn |
(n-1)*O(l) where l is max(len(s1),..,len(sn)) |
|
Difference s-t |
O(len(s)) |
|
s.difference_update(t) |
O(len(t)) |
|
Symmetric Difference s^t |
O(len(s)) |
O(len(s) * len(t)) |
s.symmetric_difference_update(t) |
O(len(t)) |
O(len(t) * len(s)) |
Dictionary
The below is the time complexity of the dictionary operations cheat sheet.
Operation |
Average Case |
Amortized Worst Case |
---|---|---|
k in d |
O(1) |
O(n) |
Copy[3] |
O(n) |
O(n) |
Get Item |
O(1) |
O(n) |
Set Item[1] |
O(1) |
O(n) |
Delete Item |
O(1) |
O(n) |
Iteration[3] |
O(n) |
O(n) |