UNIT-II
Algorithm
A process or set of rules to be followed in calculations or other problem-solving
operations, especially by a computer.
A step-by-step method of solving a problem or making decisions, as in making a diagnosis. An
established mechanical procedure for solving certain mathematical problems. Properties of the
algorithm. Finiteness. An algorithm must always terminate after a finite number of steps.
An algorithm is a procedure or formula for solving a problem, based on conductiong a
sequence of specified actions. A computer program can be viewed as an elaborate
algorithm. In mathematics and computer science, an algorithm usually means a small
procedure that solves a recurrent problem.
Algorithm and its characteristics
The characteristics of a good algorithm are:
      Precision – the steps are precisely stated (defined).
      Uniqueness – results of each step are uniquely defined and only depend on the
       input and the result of the preceding steps.
      Finiteness – the algorithm stops after a finite number of instructions are executed
                              OR
Characteristics -
1. Finiteness: An algorithm must terminate after a finite number of steps and further
each step must be executable in finite amount of time.
2. Each step of an algorithm must be precisely defined; the action to be carried out must
be rigorously and unambiguously specified for each case.
3. Input: An algorithm has zero or more, but only finite number of inputs.
Zero input example: ASCII chart of 0-255
4. Output: An algorithm has one or more output.
In mathematics, computing, linguistics and related subjects, an algorithm is a sequence
of finite instructions, often used for calculation and data processing. It is formally a type
of effective method in which a list of well-defined instructions for completing a task will,
when given an initial state, proceed through a well-defined series of successive states,
eventually terminating in an end-state. The transition from one state to the next is not
necessarily deterministic; some algorithms, known as probabilistic algorithms,
incorporate randomness.
A step-by-step method of solving a problem or making decisions, as in making a
diagnosis.
An established mechanical procedure for solving certain mathematical problems.
Properties of the algorithm
   1. Finiteness. An algorithm must always terminate after a finite number of steps.
   2. Definiteness. Each step of an algorithm must be precisely defined; the actions to
      be carried out must be rigorously and unambiguously specified for each case.
   3. Input. An algorithm has zero or more inputs, i.e, quantities which are given to it
      initially before the algorithm begins.
   4. Output. An algorithm has one or more outputs i.e, quantities which have a
      specified relation to the inputs.
   5. Effectiveness. An algorithm is also generally expected to be effective. This
      means that all of the operations to be performed in the algorithm must be
      sufficiently basic that they can in principle be done exactly and in a finite length of
      time.
Complexity of Algorithm:
It is very convenient to classify algorithms based on the relative amount of time or
relative amount of space they require and specify the growth of time /space
requirements as a function of the input size. Thus, we have the notions of:
1. Time Complexity: Running time of the program as a function of the size of input
2. Space Complexity: Amount of computer memory required during the program
execution, as a function of the input size.
Advantages -
      it is a step-by-step rep. of a solution to a given prblem ,which is very easy to
       understand.
      it easy to first develope an algorithm,&then convert it into a flowchart &then into a
       computer program.
      it is easy to debug as every step is got its own logical sequence.
Disadvantages-
it is time consuming & cubersome as an algorithm is developed first which is converted
into flowchart &then into a computer program
                                          OR
     Advantages of algorithm
        1. It is a step-wise representation of a solution to a given problem, which makes it easy to
           understand.
        2. An algorithm uses a definite procedure.
        3. It is not dependent on any programming language, so it is easy to understand for anyone
           even without programming knowledge.
        4. Every step in an algorithm has its own logical sequence so it is easy to debug.
        5. By using algorithm, the problem is broken down into smaller pieces or steps hence, it is
           easier for programmer to convert it into an actual program
     Disadvantages of algorithm.
        1. Writing algorithm takes a long time.
        2. An Algorithm is not a computer program, it is rather a concept of how a program should
           be.
     Examples of Algorithms:
     Write an algorithm to add two numbers entered by user.
        3.   Step   1:
                     Start
        4.   Step   2:
                     Declare variables num1, num2 and sum.
        5.   Step   3:
                     Read values num1 and num2.
        6.   Step   4:
                     Add num1 and num2 and assign the result to sum.
        7.           sum←num1+num2
        8.   Step 5: Display sum
        9.   Step 6: Stop
    Flowchart
    A flowchart is the graphical or pictorial representation of an algorithm with the help of
    different symbols, shapes and arrows in order to demonstrate a process or a program.
    With algorithms, we can easily understand a program. The main purpose of a flowchart is
    to analyze different processes. Several standard graphics are applied in a flowchart:
    Terminal Box - Start / End
    Input / Output
    Process / Instruction
    Decision
    Connector / Arrow
     The graphics above represent different part of a flowchart. The process in a flowchart
     can be expressed through boxes and arrows with different sizes and colors. In a
     flowchart, we can easily highlight a certain element and the relationships between each
     part.
    A flowchart is a graphical representation of decisions and their results mapped out in
    individual shapes that were first developed by Herman Goldstine and John von
    Neumann in the 1940's. Flowcharts can provide a step-by-step diagram for mapping out
complex situations, such as programming code or troubleshooting problems with
a computer. The picture to the right shows an image from Microsoft Visio, a popular
program used to design and create flowcharts and diagrams.
Below is an example of a basic flowchart created in Visio that gives a good representation
of their layout. As can be seen, you have several steps that may be followed systematically
to help determine the cause of computer problem. Of course, this is just an example and
not the full set of steps required to troubleshootcomputer issues.
Advantages of Flowchart
 1.     It is a convenient method of communication.
 2.     It indicates very clearly just what is being done, where as a
        Program has logical complexities.
 3.     A key to correct programming.
 4.     It is an important tool for planning and designing a new
        System.
 5.     It clearly indicates the role-played at each level.
 6.     It saves the inconveniences in future and serves the purpose
        of documentation for a system.
 7.     It provides an overview of the system and also demonstrate
        the relationship between various steps.
 8.     Facilitates troubleshooting.
 9.     It promote logical accuracy.
 10.    It makes sure that no logical path is left incomplete without any
        action being taken.
 Disadvantages of Flowchart
 1. Flowchart are waste of time and slow down process of software development.
 2. Flowchart are quiet costly to produce and difficult to use and manage.
 3. Flowcharts are not meant for man to computer communication
 4. The flowchart can be complex when the logic of a program is quite
        complicated.
 5. Drawing flowchart is a time-consuming task
        .
 6. Difficult to alter the flowchart. Sometimes, the designer needs to redraw the
    complete flowchart to change the logic of the flowchart or to alter the flowchart.
 7. Since it uses special sets of symbols for every action, it is quite a tedious task to
    develop a flowchart as it requires special tools to draw the necessary symbols.
8. In the case of a complex flowchart, other programmers might have a difficult time
   understanding the logic and process of the flowchart.
9. It is just a visualization of a program, it cannot function like an actual program