0% found this document useful (0 votes)
7 views

c unit4

An algorithm is a set of clear instructions designed to solve a specific problem, characterized by definiteness, input/output, finiteness, language independence, and feasibility. While algorithms have advantages such as ease of understanding and aiding in debugging, they can be challenging to write for complex problems and may be time-consuming. Additionally, concepts like time and space complexity, along with Big O notation, are essential for analyzing algorithm efficiency.

Uploaded by

atul46160
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

c unit4

An algorithm is a set of clear instructions designed to solve a specific problem, characterized by definiteness, input/output, finiteness, language independence, and feasibility. While algorithms have advantages such as ease of understanding and aiding in debugging, they can be challenging to write for complex problems and may be time-consuming. Additionally, concepts like time and space complexity, along with Big O notation, are essential for analyzing algorithm efficiency.

Uploaded by

atul46160
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

An algorithm is a set of well-defined instructions to solve a particular problem or algorithm is a step by

step instructions to solve a particular problem. It is a step-by-step procedure to execute some


instructions in a certain order to get the required output. Software Engineer commonly uses an
algorithm for planning and solving problems.

 Definiteness: Each instruction must be clear and unambiguous.

 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.

 Language Independent: The direction of steps in an algorithm should be independent of any


programming language.

 Feasible: The algorithm should be simple and generic so that it is feasible with any available
resources.

Advantages of Algorithm

 It is easy to understand because of its step-wise representation.


 As it is independent of programming language so the person with no programming language can
understand it.

 The procedural step helps the problem to break down into smaller problems, making it easier for
the programmer to understand and code.

 It is easy to reproduce the alogarithm

 It is less costly as compare to flowchart

 It is help in debugging process

 It is help to find the errors

 Algorithm can be used in future

Disadvantages of Algorithm

• It is difficult to write algorithms for large and complex program.


• It is difficult to modify for large and complex program.
• It is time consuming. Some programmers thinks that it is waste of time.
• It is not executable.
• If the algorithm is wrong then the program written based on that algorithm is also wrong.
• It is difficult to show branching and Looping in algorithm.
• It is difficult to understand large and complex problems.

1: write an algorithm to add two numbers and display the result.


Step 1: START
Step 2: Declare integer variables num1, num2, and result.
Step 3: Read values num1 and num2.
Step 4: add the value and store it to the result variable. result←num1+num2
Step 5: print result
Step 6: STOP

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.

Property of Big o notion

 Big O Notation is important because it helps analyze the efficiency of algorithms.

 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.

 Enables developers to optimize code and improve overall performance.

Summary Table of Common Big-O Notations

Notation Name Example Algorithm

O(1) Constant time Accessing array by index

O(log n) Logarithmic time Binary search

O(n) Linear time Traversing a list


Notation Name Example Algorithm

O(n log n) Linearithmic time Mergesort, heapsort

O(n²) Quadratic time Bubble sort, insertion sort

O(2ⁿ) Exponential time Recursive Fibonacci

O(n!) Factorial time Generating all permutations

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

Basic Symbols used in Flowchart Designs

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.

Rules For Creating Flowchart :

A flowchart is a graphical representation of an algorithm.it should follow some rules while


creating a flowchart
Rule 1: Flowchart opening statement must be ‘start’ keyword.
Rule 2: Flowchart ending statement must be ‘end’ keyword.
Rule 3: All symbols in the flowchart must be connected with an arrow line.
Rule 4: The decision symbol in the flowchart is associated with the arrow line.

Advantages of Flowchart:

 It is easy to understand because of its step-wise representation.

 As it is independent of programming language so the person with no programming


language can understand it.

 The procedural step helps the problem to break down into smaller problems, making it easier for
the programmer to understand and code.

 Flowcharts are a better way of communicating the logic of the system.

 Flowcharts act as a guide for blueprint during program designed.

 Flowcharts help in debugging process.

 With the help of flowcharts programs can be easily analyzed.

 It provides better documentation.

 Flowcharts serve as a good proper documentation.

 Easy to trace errors in the software.

 Easy to understand.

 The flowchart can be reused for inconvenience in the future.

 It helps to provide correct logic.

Disadvantages of Flowchart:

 It is difficult to draw flowcharts for large and complex programs.

 There is no standard to determine the amount of detail.

 Difficult to reproduce the flowcharts.

 It is very difficult to modify the Flowchart for large and complex program.

 Making a flowchart is costly.

 Some developer thinks that it is waste of time.

 It makes software processes low.

 If changes are done in software, then the flowchart must be redrawn


 Examples of flowcharts in programming
 1. Add two numbers entered by the user.

. 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.

The value ten is assigned to the variable n.

For value = zero to less than a number.

Display the numbers.

Algorithm for the Program Factorial of a Given Number.

Step 1: start

Step 2: initialize fact = 1

Step 3: input from the user value n

Step 4: for i=1 to i <= n repeat the process

Step 5: fact = fact * i

Step 6: i++ [increament i by one]

Step 7: print fact value

Step 8: stop

pseudo-code from the above algorithm.

Start program

Declare fact and n

Enter number for n

for i=1 to i <=n

Perform fact = fact * i

Display fact

End program

 variables and Assignments: Use = to assign values.

 Example: x = 5

 Conditionals: Use if, else, and end if.


 Finding the Maximum of Two Numbers:

function max(a, b)

if (a > b) then

return a

else

return b

end if

end function

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy