📚 Edexcel A-Level Programming: Algorithms, Data Structures and Core Techniques | 爱德思 A-Level 编程:算法、数据结构与核心技巧
Programming is at the heart of A-Level Computer Science. This revision guide covers the essential programming concepts, data structures and algorithms required for the Edexcel specification. It is designed to help you build a strong foundation, understand how code behaves, and apply problem-solving skills in both theory and practical assessments.
编程是 A-Level 计算机科学的核心。本复习指南涵盖爱德思考试大纲所要求的基本编程概念、数据结构和算法。它旨在帮助你打下扎实基础,理解代码的行为方式,并在理论考试和实践评估中运用问题解决能力。
1. Programming Constructs | 编程基本结构
Every program is built from three fundamental constructs: sequence, selection and iteration. Sequence means instructions are executed in the order they appear. Selection allows a program to make decisions, typically using if, else if and else statements. Iteration repeats a block of code, either for a fixed number of times or until a condition changes.
每个程序都由三种基本结构组成:顺序、选择和迭代。顺序意味着指令按照出现的先后顺序执行。选择允许程序做出决策,通常使用 if、else if 和 else 语句。迭代重复一段代码,可以是固定次数,也可以是直到某个条件发生变化。
In Edexcel pseudocode, iteration can be written as FOR loops, WHILE loops and REPEAT loops. A FOR loop is count-controlled because the number of repetitions is known before the loop starts. A WHILE loop is condition-controlled and checks the condition before each iteration. A REPEAT loop checks the condition after each iteration, so it always runs at least once.
在爱德思伪代码中,迭代可以写作 FOR 循环、WHILE 循环和 REPEAT 循环。FOR 循环是计数控制循环,因为重复次数在循环开始前已知。WHILE 循环是条件控制循环,在每次迭代之前检查条件。REPEAT 循环在每次迭代之后检查条件,因此它总是至少执行一次。
2. Data Types and Variables | 数据类型与变量
Variables are named storage locations in memory that can hold values during program execution. Each variable has a data type that determines what kind of value it can store and what operations can be performed on it. Common primitive data types include integer, real, Boolean, character and string.
变量是内存中命名的存储位置,可以在程序执行期间保存值。每个变量都有一个数据类型,它决定变量可以存储哪种值以及可以对它执行哪些操作。常见的原始数据类型包括整数、实数、布尔值、字符和字符串。
Choosing the correct data type is important because it affects memory usage and the range of values available. For example, an integer can store whole numbers, while a real data type can store numbers with fractional parts. A Boolean only stores TRUE or FALSE values, which is useful for controlling selection and iteration. A string stores a sequence of characters, such as a name or a sentence.
选择正确的数据类型很重要,因为它会影响内存使用和可用值的范围。例如,整数可以存储整数,而实数数据类型可以存储带有小数部分的数字。布尔值只存储 TRUE 或 FALSE 值,这对控制选择和迭代非常有用。字符串存储字符序列,例如姓名或句子。
3. Arrays and Lists | 数组与列表
Arrays and lists are data structures that store multiple items of the same data type under one identifier. They allow a program to manage collections of data efficiently using an index. In most Edexcel pseudocode, the first element of an array is at index 0, so an array of eight items has indices from 0 to 7.
数组和列表是在一个标识符下存储多个相同数据类型的数据结构。它们允许程序使用索引高效地管理数据集合。在大多数爱德思伪代码中,数组的第一个元素位于索引 0 处,因此包含八个元素的数组索引范围为 0 到 7。
A one-dimensional array is like a single row of values, while a two-dimensional array can be visualised as a table with rows and columns. Two-dimensional arrays are commonly used for board games, matrices and spreadsheet-like data. You should be able to declare arrays, assign values, access elements and use loops to traverse them.
一维数组就像单行值,而二维数组可以可视化为包含行和列的表格。二维数组通常用于棋盘游戏、矩阵和类似电子表格的数据。你应该能够声明数组、赋值、访问元素以及使用循环遍历它们。
- 1D array:
names[0], names[1], names[2] - 2D array:
grid[0][0], grid[0][1], grid[1][0]
中文要点:一维数组:names[0]、names[1]、names[2];二维数组:grid[0][0]、grid[0][1]、grid[1][0]。
4. String Manipulation | 字符串处理
String handling is a common skill in A-Level programming. You need to know how to find the length of a string, extract substrings, concatenate strings, and convert between upper case and lower case. These operations are frequently tested in pseudocode questions.
字符串处理是 A-Level 编程中的常见技能。你需要知道如何求字符串长度、提取子串、拼接字符串,以及进行大写和小写转换。这些操作在伪代码题中经常考查。
Concatenation joins two or more strings together using the + operator or a function such as concat. Substring extraction uses a starting position and a length to return part of a string. For example, in the string “Computer”, the substring starting at position 1 with length 3 is “omp” if indexing starts from 0. You should also be able to convert characters to their ASCII codes and back.
拼接使用 + 运算符或 concat 等函数将两个或多个字符串连接起来。子串提取使用起始位置和长度来返回字符串的一部分。例如,在字符串 “Computer” 中,如果索引从 0 开始,则从位置 1 开始、长度为 3 的子串是 “omp”。你还应该能够将字符转换为对应的 ASCII 码,以及将 ASCII 码转换回字符。
5. Functions and Procedures | 函数与过程
Functions and procedures are named blocks of code that perform a specific task. They help break a large problem into smaller, reusable parts. A function always returns a value, whereas a procedure may or may not return a value, depending on the language and context.
函数和过程是执行特定任务的命名代码块。它们有助于把大问题分解成更小、可重用的部分。函数总是返回一个值,而过程根据语言和上下文可能返回值,也可能不返回值。
Parameters are values passed into a function or procedure when it is called. They allow the same block of code to work with different data. Parameters can be passed by value or by reference. When passed by value, the original data is not modified. When passed by reference, the function can change the original variable directly.
参数是在调用函数或过程时传入的值。它们允许同一段代码处理不同的数据。参数可以按值传递或按引用传递。按值传递时,原始数据不会被修改。按引用传递时,函数可以直接更改原始变量。
6. Scope of Variables | 变量的作用域
The scope of a variable determines where in the program the variable can be accessed. A local variable is declared inside a function or procedure and can only be used within that block. A global variable is declared outside all functions and can be used anywhere in the program.
变量的作用域决定程序中哪些位置可以访问该变量。局部变量在函数或过程内部声明,只能在该代码块中使用。全局变量在所有函数之外声明,可以在程序的任何位置使用。
Local variables help prevent accidental changes because they do not interfere with other parts of the program. Global variables use memory for the entire runtime and can make debugging harder if they are changed accidentally. In Edexcel exams, you may be asked to identify the scope of a variable or explain the benefits of using local variables.
局部变量有助于防止意外更改,因为它们不会干扰程序的其他部分。全局变量在整个运行期间占用内存,如果被意外修改,会使调试更加困难。在爱德思考试中,你可能需要识别变量的作用域,或解释使用局部变量的好处。
7. Recursion | 递归
Recursion occurs when a function calls itself to solve a smaller version of the same problem. A recursive algorithm must have a base case, which stops the recursion, and a recursive case, which moves the problem towards the base case. Without a base case, the recursion would continue indefinitely and eventually cause a stack overflow.
递归发生在函数调用自身来解决同一问题的较小版本时。递归算法必须有一个基准情形来终止递归,还有一个递归情形使问题向基准情形靠近。如果没有基准情形,递归将无限进行,最终导致栈溢出。
A classic example is the factorial function. For an integer n, n! = n × (n−1)! when n > 1, and 1! = 1. This can be expressed as:
一个经典示例是阶乘函数。对于整数 n,当 n > 1 时,n! = n × (n−1)!,且 1! = 1。这可以表示为:
factorial(n) = n × factorial(n − 1) with factorial(1) = 1
Each recursive call uses memory on the call stack. Recursion is elegant for problems such as traversing trees, performing binary search and calculating Fibonacci numbers, but it can be less efficient than iteration if many repeated calls are made.
每次递归调用都会在调用栈上使用内存。递归对于遍历树、执行二分查找和计算斐波那契数等问题非常简洁,但如果产生许多重复调用,它可能比迭代效率更低。
8. Searching Algorithms | 查找算法
Searching algorithms are used to find a target value in a collection. The two most important algorithms for Edexcel A-Level are linear search and binary search. Linear search checks each element one by one from the beginning until the target is found or the end is reached.
查找算法用于在集合中查找目标值。爱德思 A-Level 最重要的两种算法是线性查找和二分查找。线性查找从头开始逐个检查每个元素,直到找到目标或到达末尾。
Linear search works on unsorted data and has a time complexity of O(n), meaning the worst-case number of checks is proportional to the number of items. Binary search is much faster on sorted data because it repeatedly divides the search space in half. The time complexity of binary search is O(log n).
线性查找适用于未排序数据,时间复杂度为 O(n),意味着最坏情况下的检查次数与数据项数量成正比。二分查找在已排序数据上要快得多,因为它反复将搜索范围缩小一半。二分查找的时间复杂度为 O(log n)。
| Algorithm | Data requirement | Time complexity |
|---|---|---|
| Linear search | Unsorted or sorted | O(n) |
| Binary search | Sorted | O(log n) |
中文对照:算法、数据要求、时间复杂度。线性查找:未排序或已排序、O(n);二分查找:已排序、O(log n)。
9. Sorting Algorithms | 排序算法
Sorting arranges data into a meaningful order, typically ascending or descending. Bubble sort works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. After each pass, the next largest element is in its correct position.
排序将数据排列成有意义的顺序,通常是升序或降序。冒泡排序通过反复比较相邻元素并在顺序错误时交换它们来工作。每一轮之后,下一个最大元素会到达正确位置。
Merge sort is a divide-and-conquer algorithm. It splits the list into halves recursively until each sublist contains one element, then merges the sublists back together in order. Merge sort has a time complexity of O(n log n), which is more efficient than bubble sort’s O(n²) for large data sets.
归并排序是一种分治算法。它递归地将列表分成两半,直到每个子列表只包含一个元素,然后将子列表按照顺序合并回去。归并排序的时间复杂度为 O(n log n),对于大数据集比冒泡排序的 O(n²) 更高效。
In an exam, you may be asked to trace a bubble sort or merge sort step by step. You should also be able to compare their efficiency and justify which algorithm is more suitable for a given scenario.
在考试中,你可能会被要求逐步跟踪冒泡排序或归并排序的过程。你还应该能够比较它们的效率,并论证在给定场景下哪种算法更合适。
10. Testing and Debugging | 测试与调试
Testing is essential to ensure that a program works correctly and meets its requirements. Black box testing focuses on inputs and outputs without looking at the internal code, while white box testing designs test cases based on the internal logic and paths of the program.
测试对于确保程序正常工作并满足需求至关重要。黑盒测试关注输入和输出,不查看内部代码;白盒测试则根据程序的内部逻辑和路径设计测试用例。
Test data should include normal data, boundary data and erroneous data. Normal data is valid and should be accepted. Boundary data tests the limits of valid ranges, such as 0 or the maximum allowed value. Erroneous data is invalid input that should be rejected gracefully without crashing the program.
测试数据应包括正常数据、边界数据和错误数据。正常数据是有效的且应该被接受。边界数据测试有效范围的极限,例如 0 或允许的最大值。错误数据是无效输入,程序应能够优雅地拒绝而不会崩溃。
Debugging is the process of finding and fixing errors in a program. Common types of error include syntax errors, logic errors and runtime errors. Using trace tables, breakpoints and test output helps identify where a program deviates from expected behaviour.
调试是查找并修复程序错误的过程。常见的错误类型包括语法错误、逻辑错误和运行时错误。使用跟踪表、断点和测试输出有助于确定程序在哪里偏离了预期行为。
Published by TutorHao | Programming Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply