Bubble Sort
Repeatedly steps through the list, compares adjacent elements and swaps them if they're in the wrong order. Simple but inefficient for large datasets.
def bubble_sort(arr):
n = len(arr)
for i in range(n):
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 arrSorting Algorithms
Bubble Sort
Repeatedly steps through the list, compares adjacent elements and swaps them if they're in the wrong order.
Insertion Sort
Builds the sorted array one item at a time by inserting each element into its correct position.
Selection Sort
Repeatedly finds the minimum element from the unsorted portion and places it at the beginning.
Merge Sort
Divides the array into halves, recursively sorts them, and merges the sorted halves back together.
Quick Sort
Selects a pivot element and partitions the array around it, recursively sorting the partitions.
Heap Sort
Builds a max heap from the array, then repeatedly extracts the maximum element to build the sorted result.
Counting Sort
A non-comparison sort that counts occurrences of each element and uses arithmetic to determine positions.
Maze Generation
Recursive Backtracking
Uses depth-first search with backtracking to carve passages. Creates long, winding corridors with few dead ends. The classic maze generation algorithm.
Prim's Algorithm
Starts from a random cell and grows the maze by adding random frontier cells. Creates mazes with more branching and shorter dead ends than DFS.
Kruskal's Algorithm
Uses Union-Find to randomly connect cells while avoiding cycles. Creates mazes with uniform passage lengths and even branching throughout.
Pathfinding Algorithms
Depth-First Search
Explores as deep as possible before backtracking. Fast but doesn't guarantee the shortest path. Performance depends heavily on maze structure.
Breadth-First Search
Explores all neighbors at current depth before moving deeper. Always finds the shortest path in unweighted graphs. Expands uniformly in all directions.
Dijkstra's Algorithm
Uses a priority queue to always expand the lowest-cost path. Guarantees shortest path in weighted graphs. In uniform mazes, behaves like BFS.
A* Search
Combines path cost with a heuristic (Manhattan distance) to guide search toward the goal. Often explores fewer cells than BFS while finding optimal paths.