以下是前端程序员在职业生涯中可能会遇到且大概率需要掌握的十个重要算法。
function bubbleSort(arr) {
let len = arr.length;
for (let i = 0; i < len - 1; i++) {
for (let j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
function quickSort(arr) {
if (arr.length <= 1) return arr;
let pivotIndex = Math.floor(arr.length / 2);
let pivot = arr.splice(pivotIndex, 1)[0];
let left = [];
let right = [];
for (let i = 0; i < arr.length; i++){
if (arr[i] < pivot){
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot], quickSort(right));
}
function 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;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
class Stack {
constructor() {
this.items = [];
}
push(item) {
this.items.push(item);
}
pop() {
if (this.isEmpty()) {
return null;
}
return this.items.pop();
}
peek() {
if (this.isEmpty()) {
return null;
}
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
}
class Queue {
constructor() {
this.items = [];
}
enqueue(item) {
this.items.push(item);
}
dequeue() {
if (this.isEmpty()) {
return null;
}
return this.items.shift();
}
front() {
if (this.isEmpty()) {
return null;
}
return this.items[0];
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
}
function bfs(graph, start) {
let visited = new Set();
let queue = [start];
while (queue.length > 0) {
let node = queue.shift();
if (!visited.has(node)) {
visited.add(node);
let neighbors = graph[node];
for (let neighbor of neighbors) {
queue.push(neighbor);
}
}
}
return visited;
}
function dfs(graph, start) {
let visited = new Set();
function traverse(node) {
if (visited.has(node)) return;
visited.add(node);
let neighbors = graph[node];
for (let neighbor of neighbors) {
traverse(neighbor);
}
}
traverse(start);
return visited;
}
function fibonacci(n) {
let fib = [0, 1];
for (let i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}
function reverseString(str) {
return str.split('').reverse().join('');
}
function isPalindrome(str) {
let reversedStr = reverseString(str);
return str === reversedStr;
}
function gcd(a, b) {
if (b === 0) {
return a;
} else {
return gcd(b, a % b);
}
}
function primeFactors(n) {
let factors = [];
let divisor = 2;
while (n >= 2) {
if (n % divisor === 0) {
factors.push(divisor);
n = n / divisor;
} else {
divisor++;
}
}
return factors;
}
function mergeSort(arr) {
if (arr.length <= 1) return arr;
let mid = Math.floor(arr.length / 2);
let leftArr = mergeSort(arr.slice(0, mid));
let rightArr = mergeSort(arr.slice(mid));
return merge(leftArr, rightArr);
}
function merge(leftArr, rightArr) {
let result = [];
let i = 0;
let j = 0;
while (i < leftArr.length && j < rightArr.length) {
if (leftArr[i] < rightArr[j]) {
result.push(leftArr[i]);
i++;
} else {
result.push(rightArr[j]);
j++;
}
}
return result.concat(leftArr.slice(i), rightArr.slice(j));
}
贪心算法是一种在每一步选择中都采取在当前状态下最好或最优解的策略,从而希望导致结果是全局最好或最优的算法。
深度优先搜索是一种用于遍历或搜索树或图的算法。它从根节点开始,逐步向下访问子节点,直到到达叶子节点或无法继续向下访问时回溯到上一个节点再进行下一步的访问,直到访问完所有节点。