1. Sorting Algorithms
Bubble Sort: A simple comparison-based algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
javafunction bubbleSort(arr) { let n = arr.length; for (let i = 0; i < n - 1; i++) { for (let j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; // Swap } } } return arr; }
Quick Sort: A divide-and-conquer algorithm that picks an element as a pivot and partitions the array around the pivot.
javasfunction quickSort(arr) { if (arr.length <= 1) return arr; let pivot = arr[Math.floor(arr.length / 2)]; let left = arr.filter(x => x < pivot); let middle = arr.filter(x => x === pivot); let right = arr.filter(x => x > pivot); return [...quickSort(left), ...middle, ...quickSort(right)]; }
2. Searching Algorithms
Linear Search: A simple search algorithm that checks each element in the list until the desired element is found.
javascriptfunction linearSearch(arr, target) { for (let i = 0; i < arr.length; i++) { if (arr[i] === target) return i; } return -1; // Not found }
Binary Search: An efficient search algorithm for sorted arrays, which repeatedly divides the search interval in half.
javafunction binarySearch(arr, target) { let low = 0; let high = arr.length - 1; while (low <= high) { let mid = Math.floor((low + high) / 2); if (arr[mid] === target) return mid; if (arr[mid] < target) low = mid + 1; else high = mid - 1; } return -1; // Not found }
3. Graph Algorithms
Depth-First Search (DFS): An algorithm for traversing or searching tree or graph data structures by exploring as far as possible along each branch before backtracking.
javascrifunction depthFirstSearch(graph, start) { let visited = new Set(); function dfs(node) { if (visited.has(node)) return; visited.add(node); console.log(node); for (let neighbor of graph[node]) { dfs(neighbor); } } dfs(start); }
Breadth-First Search (BFS): An algorithm for traversing or searching tree or graph data structures level by level.
javascrfunction breadthFirstSearch(graph, start) { let visited = new Set(); let queue = [start]; while (queue.length > 0) { let node = queue.shift(); if (visited.has(node)) continue; visited.add(node); console.log(node); for (let neighbor of graph[node]) { queue.push(neighbor); } } }
4. Dynamic Programming Algorithms
Fibonacci Sequence: Using dynamic programming to efficiently compute Fibonacci numbers.
javascripfunction fibonacci(n) { let memo = [0, 1]; for (let i = 2; i <= n; i++) { memo[i] = memo[i - 1] + memo[i - 2]; } return memo[n]; }
Knapsack Problem: Solves the problem of maximizing the total value in a knapsack of a fixed capacity.
javascriptfunction knapsack(weights, values, capacity) { let n = weights.length; let dp = Array(capacity + 1).fill(0); for (let i = 0; i < n; i++) { for (let w = capacity; w >= weights[i]; w--) { dp[w] = Math.max(dp[w], dp[w - weights[i]] + values[i]); } } return dp[capacity]; }
5. Recursion Algorithms
Factorial: A classic example of a recursive algorithm to compute the factorial of a number.
javascriptfunction factorial(n) { if (n === 0) return 1; return n * factorial(n - 1); }
Merge Sort: A divide-and-conquer algorithm that divides the array into halves, sorts each half, and then merges them.
javasfunction mergeSort(arr) { if (arr.length <= 1) return arr; let mid = Math.floor(arr.length / 2); let left = mergeSort(arr.slice(0, mid)); let right = mergeSort(arr.slice(mid)); return merge(left, right); } function merge(left, right) { let result = []; while (left.length && right.length) { if (left[0] < right[0]) result.push(left.shift()); else result.push(right.shift()); } return result.concat(left, right); }
These examples cover a range of fundamental algorithms used in JavaScript. Depending on your needs, you can implement more specialized algorithms or optimize these basic ones further
No comments:
Post a Comment