Algorithm (1)
Algorithm (1)
Tanaji Patil
1 Introduction
2 Algorithm Specifications
English-Like Language
Pseudocode
Flowchart
W HAT IS AN A LGORITHM ?
Definition: An algorithm is a finite set of instructions that, if followed, accomplishes a particular task.
4 Finiteness: If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates
after a finite number of steps.
5 Effectiveness: Every instruction must be very basic so that it can be carried out, in principle, by a person
using only pencil and paper. It must be feasible.
Algorithm specification involves describing the steps of an algorithm clearly and unambiguously.
A well-specified algorithm:
Defines the input and output clearly.
Provides a step-by-step process.
Ensures correctness and efficiency.
Three ways
English like language
Pseudocode
Flowchart
English-Like Language
Example:
1 Step 1: Start.
2 Step 2: Read the input values.
3 Step 3: If the input is invalid, show an error message.
4 Step 4: Compute the required result using the formula.
5 Step 5: Print the result.
6 Step 6: End.
Advantages:
Easy for non-technical audiences to understand.
Requires no prior knowledge of formal notations.
Useful for high-level discussions and algorithm overviews.
Disadvantages:
Can be verbose and ambiguous.
Not standardized, making it harder to translate directly into code.
May omit implementation details.
Tanaji Patil Introduction to Algorithm [email protected] 5 / 13
Algorithm Specifications Pseudocode
What is Pseudocode?
Benefits of pseudocode:
Easy to write and understand.
Independent of programming language.
Serves as a bridge between an idea and implementation.
Disadvantages of pseudocode:
Requires some familiarity with programming concepts.
May not be as intuitive as flowcharts for non-technical audiences.
Pseudocode Conventions
Blocks are indicated with matching braces: { and }. A compound statement and body of a procedure
forms a block. Statements are delimited by ;
An identifier begins with a letter. The data types of variables are not explicitly declared.
Pseudocode Conventions . . .
There are two boolean values true and f alse. In order to produce these values, the logical operators and,
or, and not and the relational operators <, ≤, =, ̸=, ≥, and > are provided.
Elements of multidimensional arrays are accessed using [ and ]. For example, if A is a two dimensional
array, the (i, j)th element of the array is denoted as A[i, j]. Array indices start at zero.
while loop:
Pseudocode Conventions . . .
for loop:
repeat-until loop:
Pseudocode Conventions . . .
if statement:
case statement:
Pseudocode Conventions . . .
Input and output are done using the instructions read and write. No format is used to specify the size of
input or output quantities.
There is only one type of procedure: Algorithm. An algorithm consists of a heading and a body. The
heading takes the form.
Algorithm Name (< parameter list >)
In this algorithm (named Max), A and n are procedure parameters. Result and i are local variables.
Flowchart Symbols