
Top 10 Python Algorithms with Time Complexity ✔
Top 10 Python Algorithms with Time Complexity
1. Bubble Sort
def bubble_sort(arr):
n = len(arr)
for i in range(n-1):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
2. Selection Sort
def selection_sort(arr):
n = len(arr)
for i in range(n-1):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
3. Insertion Sort
def insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i-1
while j >= 0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
return arr
4. Merge Sort
def merge_sort(arr):
n = len(arr)
if n <= 1:
return arr
mid = n // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
5. Quick Sort
def quick_sort(arr):
n = len(arr)
if n <= 1:
return arr
pivot = arr[0]
less = [x for x in arr[1:] if x <= pivot]
greater = [x for x in arr[1:] if x > pivot]
return quick_sort(less) + [pivot] + quick_sort(greater)
6. Binary Search
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
7. Depth-First Search (DFS)
def dfs(graph, start):
visited = set()
stack = [start]
while stack:
node = stack.pop()
if node not in visited:
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
stack.append(neighbor)
return visited
8. Breadth-First Search (BFS)
def bfs(graph, start):
visited = set()
queue = [start]
while queue:
node = queue.pop(0)
if node not in visited:
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
queue.append(neighbor)
return visited
9. Dijkstra's Algorithm
def dijkstra(graph, start):
distances = {node: float('inf') for node in graph}
distances[start] = 0
queue = [(0, start)]
while queue:
distance, node = heapq.heappop(queue)
for neighbor in graph[node]:
alt_distance = distance + graph[node][neighbor]
if alt_distance < distances[neighbor]:
distances[neighbor] = alt_distance
heapq.heappush(queue, (alt_distance, neighbor))
return distances
10. Topological Sort
def topological_sort(graph):
visited = set()
ordering = []
def visit(node):
if node not in visited:
visited.add(node)
for neighbor in graph[node]:
visit(neighbor)
ordering.append(node)
for node in graph:
visit(node)
return ordering

Keywords

Advanced Guide to C++ STL for Competitive Programming
The C++ STL is built on three fundamental components: containers, algorithms, and iterators. Understanding how these components interact and their underlying implementations is crucial for optimal usa
#1 - Basic Fundamentals - DSA Series With C++ | Akash Vishwakarma
Hi its me Akash Vishwakarma, This is 1st Article of DSA Series on Skytup. In this Article We will learn about C++ Fundamentals. This article is written with the help of Ai Tools and some of my own exp
#2 - Arrays and Strings - Data Structures and Algorithms using C++ | Akash Vishwakarma
Hi its me Akash Vishwakarma, This is Article No #2 of Our DSA Series on Skytup. In this Article We will learn about C++ Fundamentals. This article is written with the help of Ai Tools and some of my o
#3 - Linked Lists - Data Structures and Algorithms using C++ | Akash Vishwakarma
Hi its me Akash Vishwakarma, This is Article No #3 of Our DSA Series on Skytup. In this Article We will learn about Linked Lists. This article is written with the help of Ai Tools and some of my own e
#4 - Stacks and Queues - Data Structures and Algorithms using C++ | Akash Vishwakarma
Hi its me Akash Vishwakarma, This is Article No #4 of Our DSA Series on Skytup. In this Article We will learn about C++ Stack and Queues. This article is written with the help of Ai Tools and some of
#5 - Trees - Data Structures and Algorithms using C++ | Akash Vishwakarma
Hi its me Akash Vishwakarma, This is Article No #5 of Our DSA Series on Skytup. In this Article We will learn about C++ Trees. This article is written with the help of Ai Tools and some of my own expe