伪代码(Pseudocode)
伪代码(Pseudocode)原文:《Pseudocode 》by Naomi Nishimura刘建文略译(http://blog.csdn.net/keminlau) In the text and lectures, algorithms will often be expressed in pseudocode, a mixture of code and English (f
伪代码(Pseudocode)
In the text and lectures, algorithms will often be expressed in pseudocode, a mixture of code and English (for specific not necessarily good examples of particular pseudocodes, see p. 17 of the course text, or the examples in the books The Design and Analysis of Computer Algorithms by Aho, Hopcroft, and Ullman, Addison-Wesley, 1974, Computer Algorithms: Introduction to Design and Analysis by Baase, 1978, and Fundamentals of Computer Algorithms by Horowitz and Sahni, Computer Science Press, 1984). While understanding pseudocode is usually not difficult, writing it can be a challenge.
伪代码是英语与代码的混合物,理解伪代码不难,但是编写伪代码则很具有挑战性。
Why use pseudocode at all? Pseudocode strikes a sometimes precarious balance between the understandability and informality of English and the precision of code. If we write an algorithm in English, the description may be at so high a level that it is difficult to analyze the algorithm and to transform it into code.
If instead we write the algorithm in code, we have invested a lot of time in determining the details of an algorithm we may not choose to implement (as we typically wish to analyze algorithms BEFORE deciding which one to implement). The goal of writing pseudocode, then, is to provide a high-level description of an algorithm which facilitates analysis and eventual coding (should it be deemed to be a “good” algorithm) but at the same time suppresses many of the details that vanish with asymptotic notation. Finding the right level in the tradeoff between readability and precision can be tricky……
为什么一定要使用伪代码?(因为)伪代码平衡了英语 (自然语言)的易理解性和非形式所带来的模糊性与代码的精确性之的矛盾。如果我们使用英语(自然语言)编写算法,那么算法描述可能太过高层而很难对算法进 行分析并转化成实际代码;如果我们用代码编写算法,我们得花大量的时间在一些我们可能最终不会实现的代码细节上(缘于我们一般在实现算法之前看对算法逻辑 进行分析)。编写伪代码(以一个适当的表述高度描述算法)的目的是为了便于算法分析并最终实现编码,但同时抑制住在渐近记法(asymptotic notation)被抹去的大量细节。在可读性与精确性之间找到一个适当的平衡点是相当的困难(tricky:问题难以处理,需要莫大的心力)的……
Just as a proof is written with a type of reader in mind (hence proofs in undergraduate textbooks tend to have more details than those in journal papers), algorithms written for different audiences may be written at different levels of detail. In assignments and exams for the course, you need to demonstrate your knowledge without obscuring使变模糊 the big picture with unneeded detail. Here are a few general guidelines for checking your pseudocode:
好比一个(数学)证明有预定的读者类型一样,面对不同的读者,算法的编写详细程度也不同。以下是指导伪代码编写的参考原则:
1. Mimic模仿 good code and good English. Using aspects of both systems means adhering to the style rules of both to some degree. It is still important that variable names be mnemonic, comments be included where useful, and English phrases be comprehensible (full sentences are usually not necessary).
仿照好代码或好英文(的表述方式)。意思是说在某种程度上吸收二者在表述上的优点。比如,使用易记的变量名、适当的使用注释和使用好理解的英语短语等。 2. Ignore unnecessary details. If you are worrying about the placement of commas, you are using too much detail. It is a good idea to use some convention to group statements (begin/end, brackets, or whatever else is clear), but you shouldn't obsess about syntax.
忽略不必要的细节。如果你频繁地使用逗号,意味着你写得太细了。使用begin/end或括号给组合多条语句是不错的主意,不过不能被语法分心。
3. Don't belabour无必要的解释 the obvious. In many cases, the type of a variable is clear from context; unless it is critical that it is specified to be an integer or real, it is often unnecessary to make it explicit.
不要对很显然的东西作解释。很多时候,变量的类型在上下文中是很明显的,除非它的特定类型(比如是整数或实数)是至关重要,不然没必要显式说明它的类型。
4. Take advantage of programming shorthands. Using if-then-else or looping structures is more concise than writing out the equivalent in English; general constructs that are not peculiar to a small number of languages are good candidates for use in pseudocode. Using parameters in specifying procedures is concise, clear, and accurate, and hence should not be omitted from pseudocode.
使用编程的短记(shorthands)工具。使用if-then-else 或循环结构比用英语表达更简洁;一些通用的语言构造也可在伪代码中使用(FIXME)。给函数指定参数是简洁、明了和准确的,不应该在伪代码中去除。
5. Consider the context. If you are writing an algorithm for quicksort, the statement "use quicksort to sort the values" is hiding too much detail; if we have already studied quicksort in class and later use it as a subroutine in another algorithm, the statement would be appropriate to use.
考虑上下文。如果你正为一个快速排序写一个算法,那么语句“use quicksort to sort the value”就隐藏了太多的细节了;如果你已经学习过快速排序算法,而现在把它作为一个另一个算法的子过程,那么可以用那句语句来表述。
6. Don't lose sight of the underlying model. It should be possible to “see through” your pseudocode to the model below; if not (that is, you are not able to analyze the algorithm easily), it is written at too high a level.
不迷惑于算法底下的模型。看透你的伪代码底下的模型是可能的,如是不行,证明编写的太高层次了(不够详细),那样你很难分析算法。
7. Check for balance. If the pseudocode is hard for a person to read or difficult to translate into working code (or worse yet, both!), then something is wrong with the level of detail you have chosen to use.
检验(可读性与精确性)平衡性。如果伪代码很难读懂或很难翻译成源代码,那么你的伪代码编写的详细程度必定在什么地方出了问题了。
(author: Naomi Nishimura)
Kemin said:
b.弱智的人玩IDE,聪明的人玩编程语言,智慧的人玩伪代码,当然还有更高的级别,想知道?去问上帝吧。
Kemin said:
b. 看来伪代码是亲自然语言多一点,还是亲源代码多一点是没准的,也没必须太准,太受限,因为只要代码读者能花点时间进入状态,进入问题的上下文,那即便是很 “烦人”很magic的源代码也看得懂,不然自然语言并且有人讲解也听不懂看不明。所以伪代码的抽象level去到哪不是一成不变的,因人而异。
参考
- http://en.wikipedia.org/wiki/Pseudocode
- http://www.coderookie.com/2006/tutorial/the-pseudocode-programming-process/
- 伪代码的使用 http://www.comp.nus.edu.sg/~xujia/mirror/algorithm.myrice.com/algorithm/pseudocode.htm
- PSEUDOCODE STANDARD http://users.csc.calpoly.edu/~jdalbey/SWE/pdl_std.html
- 算法描述中的'Pseudocode Conventions' http://bigwhite.blogbus.com/logs/2443206.html
更多推荐
所有评论(0)