lecture4
lecture4
lecture4
3) Greedy Algorithm
In this type of algorithm, we try to solve problems by selecting the
best piece first and then working on the other pieces. For
example, to find the largest value in an array, fry finding the
largest object first and then the smaller one later. Another
example of greedy algorithm is "counting coins". Suppose you
are provided coins of Rs. 1, 2, 5, and 10 and you are asked to
count the amount of all coins. The greedy procedure will be as
follows:
i) Select Rs. 10 coin.
ii) Select Rs. 5 coin.
iii) Select Rs. 2 coin.
iv) Select Rs. 1 coin.
4) Iterative Algorithm
In this type of algorithm, in order to obtain the required result, we
start with a value and repeatedly change it in the direction of the
solution. For example, to calculate the factorial of a given
number, we start with the number and continue to decrease the
number through the algorithm till the number reaches to zero.
Example:
Following algorithm inputs values in variables A, B and C. It adds
these values and displays result.
STEP 1- START
STEP 2- INPUT VALUE IN, A
STEP 3- INPUT VALUE IN B
STEP 4- INPUT VALUE IN C
STEP 5- SUM = A + B + C
STEP 6- PRINT SUM
STEP 7- END