6
Pseudocode Guidelines for Writing Algorithms High-level description: Begin with an English description of how the algorithm works. Include a general overview of the approach and goals. You may want to include examples and diagrams of the data structures that help make the operation of the algorithm more clear. For recursive algorithms it is often useful to clearly describe the base and inductive cases that make the algorithm correct. About one or two paragraphs of text is usually enough.

Pseudocode Guidelines for Writing Algorithms High-level description: –Begin with an English description of how the algorithm works. –Include a general

Embed Size (px)

Citation preview

Page 1: Pseudocode Guidelines for Writing Algorithms High-level description: –Begin with an English description of how the algorithm works. –Include a general

Pseudocode Guidelines for Writing Algorithms

• High-level description:– Begin with an English description of how the algorithm

works.

– Include a general overview of the approach and goals.

– You may want to include examples and diagrams of the data structures that help make the operation of the algorithm more clear.

– For recursive algorithms it is often useful to clearly describe the base and inductive cases that make the algorithm correct.

– About one or two paragraphs of text is usually enough.

Page 2: Pseudocode Guidelines for Writing Algorithms High-level description: –Begin with an English description of how the algorithm works. –Include a general

Language and Syntax

• Follow basic C and/or C++ syntax. – Dynamically created structures should be allocated

using "new" and deallocated using "delete".

– Avoid the use of advanced C++ features like iterators, templates, class inheritance, etc.

– Do not rely on pre-defined classes from the Weiss book unless that is part of the assignment (as it was in the case of hw #1).

Page 3: Pseudocode Guidelines for Writing Algorithms High-level description: –Begin with an English description of how the algorithm works. –Include a general

Functions and Operators• You may assume that you have at your disposal the

basic operations of the data type you are manipulating.

• Introduce definitions for the relational operators applied to non-numeric types. – For example:

Define (F < G) for nodes F and G to hold iff (F.data < G.data)

This should be done only to clarify, never to obscure, the presentation.

Page 4: Pseudocode Guidelines for Writing Algorithms High-level description: –Begin with an English description of how the algorithm works. –Include a general

Additional Tips

• Include meaningful comments in the body of the pseudocode.

• Try to make the program as succinct and elegant as you can. This is not only good style but usually makes the algorithm easier for others to understand.

• Pseudocode should be typed (preferred) or VERY neatly handwritten.

Page 5: Pseudocode Guidelines for Writing Algorithms High-level description: –Begin with an English description of how the algorithm works. –Include a general

Lists and writing pseudocode

• Given 2 sorted lists, L1 and L2, write a procedure to compute L1 U L2 (the union of the two lists). The resulting list should be sorted as well.

Page 6: Pseudocode Guidelines for Writing Algorithms High-level description: –Begin with an English description of how the algorithm works. –Include a general

Recursive Algorithms

• Provide a recursive algorithm that given a binary tree determines the number of leaves in the tree.