虛擬碼(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 (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