Simplifying Big O Notation - A Guide to Algorithm Efficiency

| 2 min read

Understanding Big O Notation

When it comes to writing code, understanding its efficiency is paramount. That’s where Big O notation comes into play—a powerful tool to gauge how fast or slow your code is, particularly when dealing with varying amounts of data.

Big O notation provides a standardized way to express the efficiency of algorithms by describing how their execution time requirements grow as input sizes increase. In simpler terms, it offers a language to discuss and analyze the scalability and efficiency of your code.

Constant Time: O(1)

In constant time (O(1)), finding a name takes the same amount of time, regardless of the list size. It’s like having an index or a direct reference to the name, making retrieval quick and constant.

function findNameConstantTime(names, index) {
	return names[index];
}

// Usage
const nameList = ["Alice", "Bob", "Charlie", "David"];
const desiredIndex = 0;
const result = findNameConstantTime(nameList, desiredIndex);
console.log(result); // Output: Alice

Linear Time: O(n)

Linear time (O(n)) represents a linear search, checking names one by one until finding the desired one or reaching the list’s end. As the list grows, the time it takes to find a name increases linearly.

function linearTime(names, targetName) {
	for (let i = 0; i < names.length; i++) {
		const name = names[i];
		if (name === targetName) {
			return { index: i, name };
		}
	}

	return -1;
}

// Usage
const nameList = ["Alice", "Bob", "Charlie", "David"];
const desiredName = "Bob";
const result = linearTime(nameList, desiredName);
console.log(result); // Output: { index: 1, name: "Bob" }

Logarithmic Time: O(log n)

Logarithmic time (O(log n)) characterizes a binary search, where the list is sorted alphabetically. At each step, the search space is halved, efficiently narrowing down options. This process results in a time complexity of O(log n), meaning the search time decreases by half with each step. Even when dealing with a large number of names, this algorithm efficiently finds the target.

function logarithmicTime(names, targetName) {
	let low = 0;
	let high = names.length - 1;

	while (low <= high) {
		const mid = Math.floor((low + high) / 2);
		const midName = names[mid];

		if (midName === targetName) {
			return { index: mid, name: midName };
		}

		if (midName < targetName) {
			low = mid + 1;
		} else {
			high = mid - 1;
		}
	}
	return -1;
}

// Usage
const nameList = ["Alice", "Bob", "Charlie", "David"];
const desiredName = "Charlie";
const result = logarithmicTime(nameList, desiredName);
console.log(result); // Output: { index: 2, name: "Charlie" }

Quadratic Time: O(n^2)

Quadratic time (O(n^2)) arises with nested loops, where each iteration increases the overall time complexity exponentially with the square of the number of names. Imagine iterating through the list twice for each name, resulting in a time complexity proportional to the square of the list size.

function quadraticTime(names) {
	const result = [];

	for (let i = 0; i < names.length; i++) {
		const name1 = names[i];

		for (let j = 0; j < names.length; j++) {
			const name2 = names[j];
			result.push(`${i + 1} ${j + 1} - ${name1} + ${name2}`);
		}
	}

	return result;
}

// Usage
const nameList = ["Alice", "Bob", "Charlie", "David"];
const results = quadraticTime(nameList);
for (const result of results) {
	console.log(result);
}
// 1 1 - Alice + Alice
// 1 2 - Alice + Bob
// 1 3 - Alice + Charlie
// 1 4 - Alice + David
// 2 1 - Bob + Alice
// 2 2 - Bob + Bob
// 2 3 - Bob + Charlie
// 2 4 - Bob + David
// 3 1 - Charlie + Alice
// 3 2 - Charlie + Bob
// 3 3 - Charlie + Charlie
// 3 4 - Charlie + David
// 4 1 - David + Alice
// 4 2 - David + Bob
// 4 3 - David + Charlie
// 4 4 - David + David

Recap

The power of Big O notation lies in its ability to compare different algorithms. By assessing their time complexities, you can choose the most efficient one for your specific problem. This is crucial in optimizing your code for performance, especially when dealing with large datasets—a common challenge in software engineering.

Resources

Beginners Guide to Big O Notation

Big-O Notation: A Simple Explanation with Examples

Thank you!

Thank you for your time and for reading this!