ALGORITHM AND PSEUDOCODE CONVENTIONS BY: SARANYA CHANDRAMOHAN,M.SC.,M.Phil,
ALGORITHM: • Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output.
• From the data structure point of view, following are some important categories of algorithms • Search − Algorithm to search an item in a data structure. • Sort − Algorithm to sort items in a certain order. • Insert − Algorithm to insert item in a data structure. • Update − Algorithm to update an existing item in a data structure. • Delete − Algorithm to delete an existing item from a data structure.
Characteristics of an Algorithm: • Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning. • Input − An algorithm should have 0 or more well-defined inputs. • Output − An algorithm should have 1 or more well-defined outputs, and should match the desired output. • Finiteness − Algorithms must terminate after a finite number of steps. • Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step directions, which should be independent of any programming code. Example: • An algorithm to add two numbers and display the result. step 1 − START ADD step 2 − get values of a & b step 3 − c ← a + b step 4 − display c step 5 − STOP
Pseudocode: • Pseudocode is a notation system for writing algorithms. • The pseudocode notation specifies operations that a machine can perform in as human-friendly (e.g., easy to read) way as possible, while avoiding ambiguity.
PSEUDOCODE CONVENTIONS: • The following conventions must be used to present your pseudo-code 1. Give a valid name for the pseudo-code procedure. (See sample code for insertion sort at the end) 2. Use the line numbers for each line of code. 3. Use proper Indentation for every statement in a block structure. 4. 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 assignment statements. Ex: i = j or I ← j n = 2 to length[A] or n ← 2 to length[A] . 6. 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. 7. For looping or iteration use for or while statements. Always end a for loop with end for and a while with end-while. 8. The conditional expression of for or while can be written as shown in rule (4).
Sample 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 4. while i > 0 and A[i] < key // If required, use this convention for a comment 5. A[i+1] ← A[i] // Swap two elements of array. 6. i ← i –1 7. end-while 8. A[i+1] ← key 9. end-for
Algorithm and pseudocode conventions

Algorithm and pseudocode conventions

  • 1.
  • 2.
    ALGORITHM: • Algorithm isa step-by-step procedure, which defines a set of instructions to be executed in a certain order to get the desired output.
  • 3.
    • From thedata structure point of view, following are some important categories of algorithms • Search − Algorithm to search an item in a data structure. • Sort − Algorithm to sort items in a certain order. • Insert − Algorithm to insert item in a data structure. • Update − Algorithm to update an existing item in a data structure. • Delete − Algorithm to delete an existing item from a data structure.
  • 4.
    Characteristics of anAlgorithm: • Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or phases), and their inputs/outputs should be clear and must lead to only one meaning. • Input − An algorithm should have 0 or more well-defined inputs. • Output − An algorithm should have 1 or more well-defined outputs, and should match the desired output. • Finiteness − Algorithms must terminate after a finite number of steps. • Feasibility − Should be feasible with the available resources.
  • 5.
    • Independent −An algorithm should have step-by-step directions, which should be independent of any programming code. Example: • An algorithm to add two numbers and display the result. step 1 − START ADD step 2 − get values of a & b step 3 − c ← a + b step 4 − display c step 5 − STOP
  • 6.
    Pseudocode: • Pseudocode isa notation system for writing algorithms. • The pseudocode notation specifies operations that a machine can perform in as human-friendly (e.g., easy to read) way as possible, while avoiding ambiguity.
  • 7.
    PSEUDOCODE CONVENTIONS: • Thefollowing conventions must be used to present your pseudo-code 1. Give a valid name for the pseudo-code procedure. (See sample code for insertion sort at the end) 2. Use the line numbers for each line of code. 3. Use proper Indentation for every statement in a block structure. 4. 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.
  • 8.
    Ex: If (conditionalexpression) statements (see the indentation) else statements end-if 5. Use “=” or “← ” operator for assignment statements. Ex: i = j or I ← j n = 2 to length[A] or n ← 2 to length[A] . 6. 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. 7. For looping or iteration use for or while statements. Always end a for loop with end for and a while with end-while. 8. The conditional expression of for or while can be written as shown in rule (4).
  • 9.
    Sample pseudo-code forinsertion sort using the above conventions: • INSERTION-SORT(A) 1. for j ← 2 to length[A] 2. key ← A[j] 3. I ← j – 1 4. while i > 0 and A[i] < key // If required, use this convention for a comment 5. A[i+1] ← A[i] // Swap two elements of array. 6. i ← i –1 7. end-while 8. A[i+1] ← key 9. end-for