Implementing Array Algorithms · 实现数组算法
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| traversal/træˈvɜːsl/ | 遍历 | biàn lì |
| index/ˈɪndeks/ | 下标 | xià biāo |
| linear search/ˈlɪnɪə sɜːtʃ/ | 线性查找 | xiàn xìng chá zhǎo |
Standard array algorithms
- Most array tasks are a traversal 遍历 plus one of a few standard patterns.
- Sum / average: accumulate a total, then divide by
length. - Count: increment when an element matches a condition.
- Min / max: track the smallest or largest seen so far.
标准数组算法
- 大多数数组任务是一次遍历加上几个标准模式之一。
- **求和 / 平均:**累加一个总和,再除以
length。 - **计数:**当元素满足条件时加一。
- **最小 / 最大:**追踪到目前为止见过的最小或最大值。
Finding the maximum
- Start
max = a[0](the first element), then traverse from index1. if (a[i] > max) { max = a[i]; }inside the loop.- After the loop,
maxholds the largest value in the array. - Start from the first element, not
0—0could be larger than every value.
找最大值
- 让
max = a[0](第一个元素)开始,然后从下标1遍历。 - 循环里
if (a[i] > max) { max = a[i]; }。 - 循环之后,
max保存数组里最大的值。 - 从第一个元素开始,而非
0——0可能比每个值都大。
Searching for a value
- To check if a value is present, traverse and compare each element.
- Return the index 下标 where it's found, or
-1if the loop finishes without a match. if (a[i] == target) return i;inside the loop;return -1;after.- This is a linear search 线性查找 (Unit 4.14 covers it in depth).
搜索一个值
- 要检查一个值是否存在,遍历并比较每个元素。
- 返回找到它的下标,或如果循环结束仍无匹配就返回
-1。 - 循环里
if (a[i] == target) return i;;之后return -1;。 - 这是线性搜索(第 4.14 节深入讲它)。
Shifting and modifying
- Some algorithms move or change elements — e.g. shift everything left, or double each value.
- Modifying needs the indexed loop so you can assign
a[i] = .... - Watch bounds when reading
a[i+1]— the last index has no neighbor. - Trace the indices carefully to avoid an out-of-bounds access.
移动与修改
- 有些算法移动或改变元素——如把一切左移,或让每个值翻倍。
- 修改需要带下标循环,好让你能赋值
a[i] = ...。 - 读
a[i+1]时留意边界——最后一个下标没有邻居。 - 仔细追踪下标以避免越界访问。
Initialize a max/min search with the FIRST element, not 0. int max = 0; fails if every value is negative (it would wrongly report 0). Use int max = a[0]; and start the loop at index 1. And when an algorithm reads a[i+1], stop the loop at i < a.length - 1, or the last iteration reads past the end.
**用第一个元素、而非 0 初始化最大/最小搜索。**如果每个值都是负数,int max = 0; 会失败(它会错误地报告 0)。用 int max = a[0]; 并从下标 1 开始循环。而当一个算法读 a[i+1] 时,让循环在 i < a.length - 1 停止,否则最后一次迭代会读过末尾。
Finding the maximum of a:
int max = a[0];for (int i = 1; i < a.length; i++) { if (a[i] > max) max = a[i]; }- For
a = {3, 9, 5}: max becomes9.
找 a 的最大值:
int max = a[0];for (int i = 1; i < a.length; i++) { if (a[i] > max) max = a[i]; }- 对
a = {3, 9, 5}:max 变成9。
Array algorithms combine a traversal with a pattern: sum/average, count, min/max, or search (return the index or -1). Initialize a min/max with the first element, not 0. Modifying elements needs the indexed loop, and reading a[i+1] needs a tighter bound to stay in range.
数组算法把一次遍历与一个模式组合:求和/平均、计数、最小/最大,或搜索(返回下标或 -1)。用第一个元素、而非 0 初始化最小/最大。修改元素需要带下标循环,而读 a[i+1] 需要更紧的边界以保持在范围内。
Finding the maximum · 找最大值
max starts at a[0]=3, becomes 9, then stays (a = {3,9,5}). · max 从 a[0]=3 开始,变成 9,然后保持(a = {3,9,5})。
To find the maximum of an array, you should initialize max to... · 要找数组的最大值,你应把 max 初始化为……
Starting at 0 fails if all values are negative. · 如果所有值都是负数,从 0 开始会失败。
For a = {3, 9, 5}, what is the maximum value? · 对 a = {3, 9, 5},最大值是多少?
9 is the largest element. · 9 是最大的元素。
A linear search returns what if the target is not found? · 如果目标未找到,线性搜索返回什么?
By convention, -1 means 'not found'. · 按惯例,-1 意思是“未找到”。
An algorithm that reads a[i+1] should loop while... · 一个读 a[i+1] 的算法循环条件应是……
Stopping one early keeps a[i+1] in bounds. · 提前一步停止让 a[i+1] 保持在范围内。
Modifying array elements (a[i] = ...) requires the indexed loop, not for-each. · 修改数组元素(a[i] = ...)需要带下标循环,而非 for-each。
for-each can't assign back into the array. · for-each 不能赋值回数组。