Algorithmic Thinking & Big-O Complexity
Analyzing algorithm efficiency is essential for building scalable applications. We use asymptotic notations to describe computational complexity.
Big-O Notation
Big-O describes the upper bound of the running time or space complexity of an algorithm in the worst-case scenario.
Complexity Comparison Table
| Complexity | Name | Example Algorithm |
|---|---|---|
| Constant | Array Index Access | |
| Logarithmic | Binary Search | |
| Linear | Single Loop | |
| Linearithmic | Merge Sort / QuickSort | |
| Quadratic | Bubble Sort / Selection Sort |
Code Example: Binary Search
Here is a sample implementation of Binary Search in TypeScript:
function binarySearch(arr: number[], target: number): number {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1; // Not found
}