Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Chalarangelo/30-seconds-of-code/llms.txt
Use this file to discover all available pages before exploring further.
Algorithms in JavaScript
Algorithms are step-by-step procedures for solving problems. Understanding common algorithms helps you write more efficient code and solve complex problems.Searching Algorithms
Binary Search
The binary search algorithm is a fast and efficient way to find the index of a given element in a sorted array. It works by repeatedly dividing the search interval in half, narrowing down the possible locations of the element. A binary search is much faster than a linear search, especially for large arrays, as it has a time complexity ofO(log n). However, it requires the array to be sorted beforehand.
This implementation does not account for duplicate values in the array.
Linear Search
Simple search that checks every element:Sorting Algorithms
Bubble Sort
Bubble sort is a simple sorting algorithm that repeatedly steps through the array, compares adjacent elements and swaps them if they are in the wrong order. The pass through the array is repeated until the array is sorted.- Declare a variable,
swapped, that indicates if any values were swapped during the current iteration - Use the spread operator (
...) to clone the original array,arr - Use a
forloop to iterate over the elements of the cloned array, terminating before the last element - Use a nested
forloop to iterate over the segment of the array between0andi, swapping any adjacent out of order elements and settingswappedtotrue - If
swappedisfalseafter an iteration, no more changes are needed, so the cloned array is returned
O(n^2), where n is the size of the input array.
Selection Sort
Selection sort is an in-place comparison sorting algorithm. It divides the input array into a sorted and an unsorted subarray. It then repeatedly finds the minimum element in the unsorted subarray and swaps it with the leftmost element in the unsorted subarray.- Use the spread operator (
...) to clone the original array,arr - Use a
forloop to iterate over elements in the array - Use
Array.prototype.slice()andArray.prototype.reduce()to find the index of the minimum element in the subarray to the right of the current index - Perform a swap, if necessary
O(n^2), where n is the size of the input array.
Quick Sort
Efficient divide-and-conquer sorting algorithm:O(n log n) average case, O(n^2) worst case.
Merge Sort
Stable sorting algorithm using divide and conquer:O(n log n) in all cases.
Mathematical Algorithms
Fibonacci Sequence
Greatest Common Divisor (GCD)
Prime Number Check
Factorial
String Algorithms
Palindrome Check
Anagram Check
Array Algorithms
Find Maximum Subarray Sum (Kadane’s Algorithm)
Two Sum Problem
Algorithm Complexity
O(1) - Constant Time
O(1) - Constant Time
Operations that take the same amount of time regardless of input size. Example: array access by index, object property lookup.
O(log n) - Logarithmic Time
O(log n) - Logarithmic Time
Algorithms that divide the problem in half each iteration. Example: binary search.
O(n) - Linear Time
O(n) - Linear Time
Operations that scale linearly with input size. Example: linear search, array iteration.
O(n log n) - Linearithmic Time
O(n log n) - Linearithmic Time
Efficient sorting algorithms. Example: merge sort, quick sort (average case).
O(n²) - Quadratic Time
O(n²) - Quadratic Time
Nested iterations over the input. Example: bubble sort, selection sort, naive duplicate detection.
O(2^n) - Exponential Time
O(2^n) - Exponential Time
Algorithms that double with each addition to input. Example: recursive fibonacci, subset generation.
Best Practices
Choose the right algorithm
Choose the right algorithm
Understand the time and space complexity trade-offs. A simple algorithm may be better for small datasets.
Consider memoization
Consider memoization
Cache results of expensive function calls to improve performance of recursive algorithms.
Use built-in methods when possible
Use built-in methods when possible
JavaScript’s built-in methods like
Array.prototype.sort() are often optimized and faster than custom implementations.Test edge cases
Test edge cases
Always test with empty inputs, single elements, and large datasets to ensure correctness.