The following conventions must be used for pseudo-code.
- Give a valid name for the pseudo-code procedure. (See sample code for insertion sort at the end).
- Use the line numbers for each line of code.
- Use proper Indentation for every statement in a block structure.
- For a flow control statements use if-else. Always end an if statement with an end-if. Both if, else and end-if should be aligned vertically in same line.
Ex : If (conditional expression)
statements (see the indentation)
else statements
end - if
5. Use : = or " <--- " operator for assignments.
Ex: i = j or i <--- J
n = 2 to length [A] or n<--- 2 to length [A]
- Array elements can be represented by specifying the array name followed by the index in square brackets. For example, A[i] indicates the ith element of the array A.
- For looping or iteration use for or while statements. Always end a for loop with end-for and a while with end-while.
- The conditional expression of for or while can be written as shown in rule (4). You can separate two or more conditions with “and”
- If required, we can also put comments in between the symbol
/* and */.
A simple pseudo-code for insertion sort using the above conventions:
INSERTION - SORT (A)
1 . for j <--- 2 to length [A]
2. key <---A [j]
3. i <--- j - 1 _ /* insert A[j] into sorted sequence A [1...j - 1] */
4. while i > 0 and A[i] > key
5. A [i+1] <--- A[i]
6. i <--- i - 1
7. end - while
8. A[i+1] <--- key
9. end - for
0 टिप्पणियाँ:
Post a Comment