Skip to content

Algorithm

一句话解释

algorithm 是有限序列的 well-定义 steps 那个解决特定 problem or 执行任务.

用大白话说

algorithm 是一种recipe for solving a problem。 It is a precise, step-by-step procedure that takes an input, processes it through a series of defined steps, and produces an output. Every program implements one or more algorithms.

Common algorithms include sorting (bubble sort, quicksort, merge sort), searching (linear search, binary search), and pathfinding (Dijkstra, A*). Each algorithm has trade-offs: some are faster but use more memory, some are simple but slow, some work well on small data but poorly on large data.

Algorithm efficiency is measured in Big O notation — how the runtime grows as the input size grows. O(1) is constant time, O(n) is linear, O(n²) is quadratic. Understanding algorithms and their efficiency is a core programming skill.

示例

javascript
// Linear search algorithm
function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) return i;
  }
  return -1; // not found
}

console.log(linearSearch([3, 1, 4, 1, 5], 4)); // 2

Linear 搜索检查每个元素 one 通过 one 直到它 查找目标. 它是 O(n) — in worst 情况, 它检查每个元素.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表