Evaluating and Expressing algorithm
In terms of evaluating an algorithm, we need to consider the input, output, definition and effectiveness and expressing an algorithm means we need to consider the flowchart, pseudocode and program of an algorithm. using the evaluation and expressing an algorithm we can easily make a decision that which algorithm we need to select to solve a problem.
Evaluating an Algorithm
To evaluate an algorithm we need to consider these terms
Input - An algorithm always does not rely on the inputs because an algorithm can take zero or more than zero inputs. even an algorithm can take even thousand of inputs. inputs are always referred to as data that we need to process the output as a result. so the computation and performance always depend on the inputs. let's say if an algorithm needs to sort a list of items then it must need and list as an input to evaluate and produce the result.
Output - One thing is that an algorithm always produces the output because without producing the output an algorithm is not usable at all. as input-output can be one or more than one. but one output is always a must. For example, if we want to find the average of 1 to 10 numbers then it algorithm must return the average as an output.
Effectiveness - Effectiveness means an algorithm must always be as simple as that so a person can perform the complete algorithm by just reading the lines. because it always that algorithm needs to convert into a computer program. so everyone is not understanding the computer language so everyone can easily understand with algorithm steps. the main aim of an algorithm is to provide the simple steps of a difficult task.
Finiteness/Termination - an algorithm must produce an output. to produce an output we need to terminate the algorithm on a state. so in terms of evaluation, an algorithm must terminate at a point or at some cases after some finite steps. to get output through the algorithm it cannot be in an infinite loop. so when the task is done or the result is calculated it must stop to produce the result.
Note Even an algorithm can terminate at a finite set of times that vary on the case.
Definiteness - Definiteness means for an algorithm each step always needs to be clear and unambiguous. if the aim of a step of the algorithm is not clear then it must be that the whole algorithm can fail to produce the desired result or output. so for an algorithm, each step must need to process something and must be in the direction of a result.
Correctness - Correctness means an algorithm must produce the desired output. it is not always that an algorithm can produce the desired output it can give the incorrect/wrong output. so let's say we want to add two numbers and print the result but instead of printing the sum of both numbers, it will print the multiplication of both numbers. this algorithm still produces the result but not the correct one.
So to evaluate an algorithm we must need to consider all the things like how many and which type of inputs the algorithm needs whether it is producing the correct output or not, it is effective or not, it is terminating on a certain case or not or it is simple as in reading and correct.
Expressing an Algorithm
To express an algorithm to others we need to use some steps. we can represent an algorithm in different ways like pseudocode, flowchart, English sentences or program etc. so we need to select a way to represent the algorithm in precise terms.
To express an algorithm we need to use some case
- Every algorithm needs a complete meaningful name like we want to write an algorithm for adding two numbers then the name of the algorithm should be "algorithm for adding two numbers" or "algorithm to print sum of two numbers".
- Inputs that we provide to an algorithm must be written in the parentheses with the name of the input.
- To assign a value to a variable we need to use the assignment operation "=". for example if we want to add 0 to a variable x then we can write like this x=0.
- For writing a comment we need to use the "//" double slash. always remember that comments are only for the people and not for execution.
- To write a conditional statement we need to use If-Then and If-Then-Else statements and to end the statement End IF is used to end the If statement.
- To write the loop we need to use While-End While keywords or you can also use For-End For keyword as well.
- We need to use the square brackets for defining the Array-like Arr[i].
- To return the result from an array we need to use the Return statement.
Let's take an example we need to write an algorithm to find the sum of n numbers. we are going to write an algorithm in the simple English word
Step - 1 Take n numbers of elements
Step - 2 Declare a variable SUM
Step - 3 Set SUM variable to zero
Step - 4 Add the number to SUM
Step - 5 Repeat Step 4 from the first value to the last value.
Step - 6 Return SUM variable
Now let's write a pseudocode for the above-given algorithm. because the above algorithm is for people who don't understand the programming language.
// Numbe is the array of n numbers
// n is the size of array
//set the S variable to zero
1. S : = 0
2. For : = 1 to n
3. S : = S + Number[i] // Here we are adding the each value to current sum
4. End For
5. Return S