Algorithms
Understand what algorithms are, how to measure their efficiency, and explore common sorting and searching techniques.
What is an Algorithm?
An algorithm is a step-by-step set of instructions for solving a problem. Every program you use is built on algorithms ā from Google Search ranking pages to your phone's GPS finding the fastest route.
Time & Space Complexity (Big O Notation)
Big O Notation describes how an algorithm's performance scales as input size grows. Common complexities from fastest to slowest:
- O(1) ā Constant time. Array index access.
- O(log n) ā Logarithmic. Binary search.
- O(n) ā Linear. Looping through an array.
- O(n log n) ā Merge sort, quicksort (average).
- O(n²) ā Quadratic. Bubble sort, nested loops.
- O(2āæ) ā Exponential. Brute-force recursion.
Sorting Algorithms
- Bubble Sort ā Repeatedly swaps adjacent elements. Simple but O(n²). Good for teaching only.
- Selection Sort ā Finds the minimum and places it at the front. Also O(n²).
- Insertion Sort ā Builds sorted array one item at a time. O(n²) worst, O(n) best.
- Merge Sort ā Divides and conquers. Always O(n log n). Stable sort.
- Quick Sort ā Picks a pivot and partitions. O(n log n) average, O(n²) worst.
- Heap Sort ā Uses a heap data structure. Always O(n log n).
Searching Algorithms
- Linear Search ā Check each element one by one. O(n). Works on unsorted data.
- Binary Search ā Halves the search space each step. O(log n). Requires sorted data.
Graph Algorithms
- BFS (Breadth-First Search) ā Explores all neighbors level by level. Used for shortest path in unweighted graphs.
- DFS (Depth-First Search) ā Goes as deep as possible before backtracking. Used for cycle detection and topological sort.
- Dijkstra's Algorithm ā Finds the shortest path in weighted graphs.
What's Next?
Practice with real code in our Web Tutorials, or explore Software Engineering to see how algorithms fit into larger systems.