Skip to content

Commit 50ccd2f

Browse files
authored
Create README.md
1 parent 6deacd6 commit 50ccd2f

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

time-complexity/README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Time Complexity in Python
2+
3+
This repository aims to provide an understanding of time complexity, a crucial concept in computer science that helps in analyzing the efficiency of algorithms. By understanding time complexity, developers can write more efficient code and make informed decisions about which algorithms to use for specific tasks.
4+
5+
## Table of Contents
6+
7+
- [Overview](#overview)
8+
- [Big O Notation](#big-o-notation)
9+
- [Common Time Complexities](#common-time-complexities)
10+
- [Constant Time: O(1)](#constant-time-o1)
11+
- [Logarithmic Time: O(log n)](#logarithmic-time-olog-n)
12+
- [Linear Time: O(n)](#linear-time-on)
13+
- [Log-Linear Time: O(n log n)](#log-linear-time-on-log-n)
14+
- [Quadratic Time: O(n^2)](#quadratic-time-on2)
15+
- [Cubic Time: O(n^3)](#cubic-time-on3)
16+
- [Exponential Time: O(2^n)](#exponential-time-o2n)
17+
- [Factorial Time: O(n!)](#factorial-time-on)
18+
- [Examples in Python](#examples-in-python)
19+
- [How to Analyze Time Complexity](#how-to-analyze-time-complexity)
20+
- [Contributing](#contributing)
21+
- [License](#license)
22+
23+
## Overview
24+
25+
Time complexity is a way to express how the running time of an algorithm grows with the size of the input. It is usually expressed using Big O notation, which classifies algorithms according to their growth rates.
26+
27+
## Big O Notation
28+
29+
Big O notation is a mathematical notation used to describe the upper bound of an algorithm's running time. It provides a high-level understanding of the algorithm's performance by focusing on the most significant terms and ignoring constants and less significant terms.
30+
31+
## Common Time Complexities
32+
33+
### Constant Time: O(1)
34+
35+
An algorithm has constant time complexity if its running time does not depend on the size of the input.
36+
37+
```python
38+
def constant_time_example(arr):
39+
return arr[0]
40+
```
41+
42+
### Logarithmic Time: O(log n)
43+
44+
An algorithm has logarithmic time complexity if its running time grows logarithmically with the input size.
45+
46+
```python
47+
def logarithmic_time_example(arr, target):
48+
low, high = 0, len(arr) - 1
49+
while low <= high:
50+
mid = (low + high) // 2
51+
if arr[mid] == target:
52+
return mid
53+
elif arr[mid] < target:
54+
low = mid + 1
55+
else:
56+
high = mid - 1
57+
return -1
58+
```
59+
60+
### Linear Time: O(n)
61+
62+
An algorithm has linear time complexity if its running time grows linearly with the input size.
63+
64+
```python
65+
def linear_time_example(arr, target):
66+
for i in range(len(arr)):
67+
if arr[i] == target:
68+
return i
69+
return -1
70+
```
71+
72+
### Log-Linear Time: O(n log n)
73+
74+
An algorithm has log-linear time complexity if its running time grows in proportion to n log n.
75+
76+
```python
77+
def log_linear_time_example(arr):
78+
if len(arr) > 1:
79+
mid = len(arr) // 2
80+
L = arr[:mid]
81+
R = arr[mid:]
82+
log_linear_time_example(L)
83+
log_linear_time_example(R)
84+
i = j = k = 0
85+
while i < len(L) and j < len(R):
86+
if L[i] < R[j]:
87+
arr[k] = L[i]
88+
i += 1
89+
else:
90+
arr[k] = R[j]
91+
j += 1
92+
k += 1
93+
while i < len(L):
94+
arr[k] = L[i]
95+
i += 1
96+
k += 1
97+
while j < len(R):
98+
arr[k] = R[j]
99+
j += 1
100+
k += 1
101+
```
102+
103+
### Quadratic Time: O(n^2)
104+
105+
An algorithm has quadratic time complexity if its running time grows quadratically with the input size.
106+
107+
```python
108+
def quadratic_time_example(arr):
109+
n = len(arr)
110+
for i in range(n):
111+
for j in range(i + 1, n):
112+
if arr[i] > arr[j]:
113+
arr[i], arr[j] = arr[j], arr[i]
114+
```
115+
116+
### Cubic Time: O(n^3)
117+
118+
An algorithm has cubic time complexity if its running time grows cubically with the input size.
119+
120+
```python
121+
def cubic_time_example(matrix):
122+
n = len(matrix)
123+
for i in range(n):
124+
for j in range(n):
125+
for k in range(n):
126+
matrix[i][j] += matrix[i][k] * matrix[k][j]
127+
```
128+
129+
### Exponential Time: O(2^n)
130+
131+
An algorithm has exponential time complexity if its running time doubles with

0 commit comments

Comments
 (0)