Triển khai Thuật toán Mảng
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| traversal/træˈvɜːsl/ | duyệt |
| index/ˈɪndeks/ | chỉ mục |
| linear search/ˈlɪnɪə sɜːtʃ/ | tìm kiếm tuyến tính |
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.
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.

Finding the maximum of an array, traced pass by pass
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).
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.
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.
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.
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.
Tìm giá trị lớn nhất
max bắt đầu ở a[0]=3, trở thành 9, sau đó giữ nguyên (a = {3,9,5}).
Để tìm giá trị lớn nhất của mảng, bạn nên khởi tạo max là...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Bắt đầu từ 0 sẽ thất bại nếu tất cả giá trị âm.
Với a = {3, 9, 5}, giá trị lớn nhất là gì?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
9 là phần tử lớn nhất.
Tìm kiếm tuyến tính trả về gì nếu mục tiêu không tìm thấy?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Theo quy ước, -1 nghĩa là 'không tìm thấy'.
Thuật toán đọc a[i+1] nên lặp khi...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Dừng sớm một bước giữ cho a[i+1] nằm trong giới hạn.
Thay đổi phần tử mảng (a[i] = ...) yêu cầu vòng lặp có chỉ số, không phải for-each.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
for-each không thể gán lại vào mảng.