c unit4
c unit4
Input: An algorithm may have 0(zero) inputs or may have well-defined inputs.
Output: Each algorithm is expected to produce one or more than one desired output.
Finiteness: once the algorithm is executed, the algorithm must terminate after a finite number
of steps.
Feasible: The algorithm should be simple and generic so that it is feasible with any available
resources.
Advantages of Algorithm
The procedural step helps the problem to break down into smaller problems, making it easier for
the programmer to understand and code.
Disadvantages of Algorithm
Problem 2: write an algorithm to calculate the factorial of a number and print the result.
Step 1: START
Step 2: Declare num, fact, and i.
Step 3: Initialize fact=1 and i=1
Step 4: Read n
Step 5: if i <= num go to step 6 else go to step 8
Step 6: Calculate fact = fact * i
Step 7: i++ and go to step 5
Step 8: Print fact
Step 9: STOP
Complexity of an algorithm
Algorithms complexity refers how much time and memory required by algorithms to solve a
particular problem.
The time and space are the two main factors that decides the complexity of the algorithm.
Time factor: This time factor of an algorithm is calculated by counting the number of key
operations such as comparisons in the sorting algorithm.
Space Factor: This space factor of an algorithm is calculated by counting the maximum memory
space required by the algorithm.
Space Complexity
Space complexity refers to the total amount of memory required by algorithms for its complete
execution or Space complexity refers to the amount of memory that an algorithm uses in its life
cycle to get the result. The calculation of space complexity is done on the basis of the following
two components.
Fixed Part: This space is required by the algorithm to store input variables, output variables,
program size, etc. those size are independent of the problem For example :- array etc.
Variable Part: This is the space required to store variables that are dependent on the size of the
problem. For example, temporary variables, dynamic memory allocation, recursion stack space,
etc.
Time Complexity
Time complexity refers to the total amount of time is required by an algorithm to its complete
execution or Time complexity refers to the amount of time that an algorithm required in its life
cycle for the completion of the task. This can be for the operations, conditional if-else
statements, etc. The calculation of space complexity is done on the basis of the following two
components.
Constant time part: Instruction that is executed only once such as input, output, if-else, switch,
etc.
Variable Time Part: Instruction that is executed more than once such as loops, recursion, etc.
Big o notation
Big O notation is a powerful tool used in computer science to describe the time complexity or space
complexity of algorithms.
Big-O, commonly referred to as “Order of”, is a way to express the upper bound of an algorithm’s time
complexity, since it analyses the worst-case situation of algorithm. It provides an upper limit on the time
taken by an algorithm in terms of the size of the input. It’s denoted as O(f(n)), where f(n) is a function
that represents the number of operations (steps) that an algorithm performs to solve a problem of
size n.
Big O notation only describes the asymptotic behavior of a function, not its exact value.
The Big O notation can be used to compare the efficiency of different algorithms or data
structures.
It provides a way to describe how the runtime or space requirements of an algorithm grow as
the input size increases.
Allows programmers to compare different algorithms and choose the most efficient one for a
specific problem.
Helps in understanding the scalability of algorithms and predicting how they will perform as the
input size grows.
flowchart
Flowchart is a graphical representation of an algorithm. . It is a step-by-step procedure to execute some
instructions in a certain order to get the required output. Programmers often use it as a program-
planning tool to solve a problem
Terminal/Terminator : Indicates the starting or ending of the program or oval symbol indicates Start,
Stop and Halt in a program’s logic flowTerminal is the first and last symbols in the flowchart.
Input/Output : It is used for any type of input /output operations or A parallelogram denotes any
function of input/output type. Program instructions that take input from input devices and display
output on output devices are indicated with parallelogram in a flowchart.
Processing: Indicates the any type of internal operations inside the processor/memory or box
represents arithmetic instructions. All arithmetic processes such as adding, subtracting,
multiplication and division are indicated by action or process symbol.
Decision : It is used for decision making such as yes/no or true/false questions or Diamond
symbol represents a decision point. Decision based operations such as yes/no question or
true/false are indicated by diamond in flowchart.
Connectors: It is used to draw a flowchart without intersecting a line or Whenever flowchart
becomes complex or it spreads over more than one page, it is useful to use connectors to avoid
any confusions. It is represented by a circle.
Flow lines: It is indicated the flow of execution or Flow lines indicate the exact sequence in
which instructions are executed. Arrows represent the direction of flow of control and
relationship among different symbols of flowchart.
Advantages of Flowchart:
The procedural step helps the problem to break down into smaller problems, making it easier for
the programmer to understand and code.
Easy to understand.
Disadvantages of Flowchart:
It is very difficult to modify the Flowchart for large and complex program.
. Find the largest among three different numbers entered by the user.
Pseudo-Code
The pseudocode in C is an informal way of writing a program for better human understanding. It is
written in simple English, making the complex program easier to understand.
Pseudocode cannot be compiled or interpreted. It doesn't follow the programming language's syntax; it
is thus written in pseudocode so that any programmer or non-programmers can easily understand it.
Int n = 10
for( i=0;i<n;i++)
printf(n);
The above source code is converted into a pseudo-code to understand in a better way.
Step 1: start
Step 8: stop
Start program
Display fact
End program
Example: x = 5
function max(a, b)
if (a > b) then
return a
else
return b
end if
end function