Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
697 views128 pages

Module 1 UNIT 1 CS1021

The document outlines the course CSE1021: Introduction to Problem Solving and Programming at VIT Bhopal University, detailing key topics such as problem analysis, algorithm development, flowcharting, coding, compilation, and debugging. It emphasizes the importance of understanding problem-solving techniques and the differences between algorithms and programs, as well as the use of high-level and low-level programming languages. Additionally, it introduces tools like pseudocode and flowcharts for effective communication and documentation in programming.

Uploaded by

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

Module 1 UNIT 1 CS1021

The document outlines the course CSE1021: Introduction to Problem Solving and Programming at VIT Bhopal University, detailing key topics such as problem analysis, algorithm development, flowcharting, coding, compilation, and debugging. It emphasizes the importance of understanding problem-solving techniques and the differences between algorithms and programs, as well as the use of high-level and low-level programming languages. Additionally, it introduces tools like pseudocode and flowcharts for effective communication and documentation in programming.

Uploaded by

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

VIT Bhopal University

Bhopal-Indore Highway, Kothrikalan, Sehore,


Madhya Pradesh – 466114.

Course Code
CSE1021
Introduction to Problem Solving
and Programming

Lecture -1 Credits 4
Unit -1
(8-Hours)

VIT Bhopal
01
Unit
Introduction to Computer Problem
Solving
Mapping Course outcome
ASSESMENT DETAILS
Unit -1
Introduction to Computer Problem Solving:

● Introduction – Problem Solving Aspect


● Top-Down Design
● Implementation of Algorithms (flowcharts, pseudo
code, programming language)
● Program Verification
● Efficiency of Algorithms
● Analysis of Algorithms.
Introduction
• Number of problems in our daily life.
• Suppose we have to calculate Simple Interest.
• Suppose we have to prepare a mark sheet.

❑ A computer is a DUMB machine.


❑ A computer cannot do anything alone without software
i.e. Program
Types of
Computer System Software
Software Example:-Operating
Overview system is the type of
system software
Describe here the
topic of the section Application Software
Example is Microsoft Office
Programming
Topic Features Languages
C, C++,java,
Python
Steps in problem solving
Problem Analysis

Algorithm Development

Flowcharting

Program Coding

Compilation and Execution

Debugging and Testing

Documentation
N1 = int(input("enter the first no:"))
N2 = int(input("enter the second no:"))
subtraction = N1 - N2
print("the sum is:",subtraction)
Problem Analysis
• Process of becoming familiar with the problem.

• We need to analyze and understand it well before solving.

• The user’s requirements cannot be fulfilled without clear


understanding of his/her problem in depth.

• Inadequate identification of problem may cause program less


useful and insufficient.

• Example: Banking Solution, Hospital Medical Study


Algorithm Development
● A procedure for solving a mathematical problem in a finite
number of steps that may frequently involves repetition of an
operation.
OR
● A step-by-step procedure for solving a problem or
accomplishing some end
OR
● Developing an algorithm is a step of program design.
Differences Between Program
And Algorithm
An Example of algorithm to find sum of two numbers
Step 1: Start

Step 2: Assume two numbers x and y and a variable sum=0

Step 3: Add two numbers x and y; store the value in variable sum

Step 4: If you want to try again with different numbers then goto step 2
else
goto step 5
Step 5: END

An algorithm to find sum of two
Step 1: Start
numbers:
Step 2: Declare variables num1, num2 and sum. Step 3: Read
values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum Step 6: Stop
Flowcharting
• Graphical representation of an algorithm using standard symbols.
• Includes a set of various standard shaped boxes that are
interconnected by flow lines.

• Flow lines have arrows(direction of flow).

• Activities are written within boxes in English.

• Communicates between programmers and business persons.


START
Start
Read two
Numbers, A and B
Input

C=A+B Processing

Display C as Sum of A and B


Output
END
End
Coding
• The process of transforming the program logic design into computer
language format.

• An act of transforming operations in each box of the flowchart in terms of


the statement of the program.

• The code written using programming language is also known


● as source code.

• Coding isn’t the only task to be done to solve a problem using computer.

• Anyone can code. TRUST ME!!


Compilation
• Process of changing high level language into machine level
language.

• It is done by special software, COMPILER

• The compilation process tests the program whether it


● contains syntax errors or not.

• If syntax errors are present, compiler can not compile the


code.
Execution
• Once the compilation is completed then the program is linked
with other object programs needed for execution, there by
resulting in a binary program and then the program is loaded
in the memory for the purpose of execution and finally it is
executed.

• The program may ask user for inputs and generates outputs
after processing the inputs.
Debugging and Testing
• Testing ensures that program performs correctly the required task.

• Verification ensures that program does what the programmer


intends to do.

• Validation ensures that the program produces the correct results


for a set of test data.

• Test data are supplied to the program and output is observed.

• Expected output = Error free


Types or levels of Programming
Languages
• High Level

● Low-level
– Machine Level
– Assembly Level
Problem Solving
● Solving problems is the core of computer science.

● Programmers must first understand how a human solves a


problem.

● Then understand how to translate this "algorithm" into


something a computer can do.

● finally how to "write" the specific syntax (required by a


computer) to get the job done.
● It is sometimes the case that a machine will solve a problem in a
completely different way than a human.
Machine Language
ADVANTAGE DISADVANTAGE
Computer directly understands Difficult to use
machine instructions
Directly starts executing Machine dependent
Takes less execution time Difficult to Debug and modify

It is relatively easy for writing programs in assembly languages, but is


slow in execution as it has to be converted into machine language
before execution.
Some examples of instructions for
assembly languages are as follows:
CODE Meaning
ADD Addition
SUB Subtraction
INR Increase
DCR Decrease
CMP Compare
High Level Language
• User friendly, Similar to natural languages
• Platform independent
• Easy to write or remember
• Easy to learn and work
• While execution: translated into assembly language then to
machine language.
• Slow in execution but is efficient for developing programs.
• Ex: C, C++, Python, Java etc.
High Level Language

Advantages Disadvantages
Easy to use More execution time
Portability Needs own translator
Easy Debugging
Easy and Fast Development of
software
Compiler
• A high level source program must be translated into a form machine can
understand. This done by software called the compiler.

• Source code => Machine language code(Object code)

• During the process of translation, the compiler reads the source


● programs statement-wise and checks for syntax errors.

• In case of any error, the computer generates message about the error.

• Ex: C, C++, Java, FORTRAN, pascal etc.


Interpreter
• Like compiler, it is also a translator which translates high
level to machine level language.

• Translates and executes the program line by line.

• Each line is checked for syntax error and then converted to


● the equivalent machine code.

• Ex. QBASIC, PERL, PHP, ASP, PYTHON, RUBY


Compiler Interpreter

Difference between compiler and


Compiler scans the entire program before Interpreter translates and executes the
translating it into machine code program line by line
interpreter
Syntax errors are found only after the compilation Syntax errors can be trapped after translations of
of complete programs every line

It takes less amount of time to analyze the source


It takes large amount of time to analyze the
code but the overall execution time is slower.
source code but the overall execution time is
comparatively faster.

No intermediate object code is generated,


Generates intermediate object code which
hence are memory efficient.
further requires linking, hence requires more
memory.
Top-Down Design

● If you have a big problem to solve, then a very


effective method of working towards a solution
is to break it down into smaller, more
manageable problems.
● This is the idea behind 'top-down design'.
Another term used is 'Stepwise refinement'.
Example

A software program is needed


to handle the lights in a
concert stadium. There are
hundreds of lights, all
producing complicated
sequences and effects.
Approach
Before a single line of code is
written, a structure chart is
drawn. Something like this:
Conti….
• A 'module' is ideally a self-contained block of
code.
• The code within has a clearly defined set of inputs
and outputs, this is called an 'interface'.
• Each module has a specific function to perform. Its
actions are controlled by a master program loop.
• This is like the conductor in an orchestra, making
the whole thing work in harmony.
Conti..
● The control module is concerned with
controlling the hardware. It does this by
communicating and controlling a number
of sub-modules.
● These sub-modules are responsible for
certain types of light.
What is an ● It is a step-by-step
algorithm? instruction for solving a
▪ An algorithm defined as a particular task in finite
sequence of definite and amount of time.
effective instructions, which
terminates with production
of correct output with given
input. • Algorithm is an effective
method for solving a problem
expressed as finite sequence
of instructions.
Writing the
Algorithm
● Algorithm is generally ● Sometimes algorithms are
developed before the actual written using pseudocodes,
coding is done. i.e. a language similar to the
● It is written using English programming language to be
like language so that it is used.
easily understandable even
by non-programmers.
Writing algorithm for solving a
problem offers these advantages

● Promotes effective communication between team
members
● Enables analysis of problem at hand
● Acts as blueprint for coding
● Assists in debugging
● Becomes part of software documentation for future
reference during maintenance phase
characteristics of a good and
correct algorithm −
● Has a set of inputs
● Steps are uniquely defined
● Has finite number of steps
● Produces desired output
Here is the algorithm for going to
the market to purchase a pen.
Step 1:- Get dressed to go to market.
Step 2:- Check your wallet for money.
Step 3:- is there is no money in the wallet replenish it.
Step 4:- Go to the shop.
Step 5:- Ask for your favorite brand of pen.
Step 6:- If pen is not available, go to step 7else to step 10.
Step 7:- Give money to shopkeeper.
Step 8:- Keep the purchased pen safely.
Step 9:- Go back to home.
Step 10:- Ask for other brand of pen.
Step 11:- Go to Step 7.
Let us now create an algorithm to
check whether a number is positive
or negative.

1. Print “ Give any number”


2. Read num
3. if(num==0) print “You entered 0”
4. if (num>0) print “you entered a positive number”
5. if (num<0) print “You entered a negative number”
Questions
1. Write an Algorithm for calculating Square of a number.
2. Write an Algorithm to multiply two numbers.

Answer 1: solution Answer 2:- solution


1) Start 1) Start
2) Give input as a number 2) Accept Number one
3) Read the number 3) Accept Number two
4) Self Multiply of input 4) Multiply both the numbers
5) Print the result. 5) Print the result.
6) End 6) End
Algorithm Development Steps
What is
Flowchart?
● A flowchart is a ● A flowchart can be
diagrammatic helpful for both writing
representation of an programs and explaining
algorithm, a step-by-step the program to others.
approach to solving a task.
FLOW CHARTS

A flow chart is a
step by step diagrammatic representation of the logic paths to

solve a given problem.


visual or graphical representation of an algorithm.

Pictorial representation of the methods to be used to solve a


given problem.
help a great deal to analyze the problem and plan its solution in

a systematic and orderly manner.


ADVANTAGES OF FLOW CHARTS

1. The flowchart shows the logic of a problem displayed in pictorial


fashion which felicitates easier checking of an algorithm.
2. The Flowchart is good means of communication to other users. It is
also a compact means of recording an algorithm solution to a problem.
3. The flowchart allows the problem solver to break the problem into
parts. These parts can be connected to make master chart.
4. The flowchart is a permanent record of the solution which can be
consulted at a later time.
EXAMPLE 3
A=10
Que: Draw flowchart to test a number for even or odd? A==10
SYMBOLS USED IN FLOW CHART

Oval:
Also called ‘Terminator’ symbol. Used to
indicate start and end of the flow.

Input and output indicators:


Also called ‘Data’ symbol. Statements like
INPUT, READ and PRINT are represented in
these Parallelograms.
SYMBOLS USED IN FLOW CHART

Process:
Also called ‘Action’ symbol. It indicates a
process action such as for storing
arithmetic operations.

Decision Maker:
A decision or branching point, usually a
yes/no or true/false question is asked, and
based on the answer, the path gets split
into two branches.
SYMBOLS USED IN FLOW CHART

Arrow or Flow Lines:


Connector to show order of flow between shapes, or
the direction being followed in the flowchart.
EXAMPLE

Draw a flowchart to solve the problem of a non-functioning lamp?


EXAMPLE 2

Que: Draw a flowchart to find out the biggest of the three unequal positive
numbers.
PseudoCode
PSEUDO CODE

• A pseudocode (pronounced Soo-doh-kohd, the word “pseudo” means “not


real,” so “pseudocode” means “not real code”. ) is another way of
representing an algorithm.
• Its is neither an algorithm nor a program. It is an abstract form of a
program.
• It consists of English like statements which perform the specific operations.
• It helps programmers to write algorithm.
• It is a detailed description of instructions that a computer must follow in a
particular order.
• Advantages: * Easy to read, * Easy to understand, * Easy to modify.
How to write a Pseudo-code?

• Elaborate everything which is going to happen in the


actual code. Don’t make the pseudo code abstract.

• Use standard programming structures such as ‘if-then’,


‘for’, ‘while’, ‘cases’ the way we use it in programming.
• Check whether all the sections of a pseudo code is
complete, finite and clear to understand and
comprehend.
• Don’t write the pseudo code in a complete
programmatic manner. It is necessary to be simple to
understand even for a layman or client, hence don’t
incorporate too many technical terms.
Advantages of Pseudocode

I. Improves the readability of any approach. It’s one of the


best approaches to start implementation of an algorithm.

II. Acts as a bridge between the program and the algorithm


or flowchart.
III. Also works as a rough documentation, so the program of
one developer can be understood easily when a pseudo
code is written out. In industries, the approach of
documentation is essential.
IV. The main goal of a pseudo code is to explain what
exactly each line of a program should do, hence making
the code construction phase easier for the programmer.
Disadvantages of Pseudocode

I. Pseudocode does not provide a visual representation


of the logic of programming.
II. There are no proper format for writing the for
pseudocode.
III. In Pseudocode their is extra need of maintain
documentation.
IV. In Pseudocode their is no proper standard very
company follow their own standard for writing the
pseudocode.
PSEUDO CODE EXAMPLE

Que: Write a pseudo code to perform the basic arithmetic operations.


Read n1, n2
Sum = n1 + n2
Diff = n1 – n2
Mult = n1 * n2
Quot = n1/n2
Print sum, diff, mult, quot
End.
PSEUDO CODE EXAMPLE

● INPUT
● COMPUTE
● PRINT
● INCREMENT
● DECREMENT
● IF/ELSE
● WHILE
● TRUE/FALSE
An imperative language uses a sequence of statements to determine how
to reach a certain goal.

Object Oriented programming (OOP) is a programming paradigm that


relies on the concept of classes and objects. It is used to structure a
software program into simple, reusable pieces of code blueprints (usually
called classes), which are used to create individual instances of objects.

Prolog is a logic programming language. It has important role in artificial


intelligence. Unlike many other programming languages, Prolog is intended
primarily as a declarative programming language. In prolog, logic is expressed
as relations (called as Facts and Rules).

Functional programming languages are specially designed to handle symbolic


computation and list processing applications. Functional programming is
based on mathematical functions.
1. JavaScript 3. HTML
2. Python
What this language is used for: What this language is used for:
What this language is used for:
Web development Web documents
Back end development
Game development Website development
Data science
Mobile apps Website maintenance
App development
Building web servers

4. CSS 5. Java
What this language is used for: 6. SQL
What this language is used for: What this language is used for:
Web documents E-commerce
Website development Database management
Finance Sales reports
Website design App development Business management
Program verification
● A formal proof system for proving programs correct.
● A proof can provide confidence of correctness in a
situation where exhaustive semantic checking is
time-consuming or impossible
Errors Checked by Compiler:-
Syntax errors:
● Errors that occur when you violate the rules of writing
C/C++ syntax are known as syntax errors. This compiler
error indicates something that must be fixed before the
code can be compiled. All these errors are detected by
compiler and thus are known as compile-time errors.
Most frequent syntax errors are:
○ Missing Parenthesis (})

○ Printing the value of variable without declaring it

○ Missing semicolon like this:


Run-time Errors :
● Errors which occur during program execution(run-time)
after successful compilation are called run-time
errors.
● One of the most common run-time error is division by
zero also known as Division error.
● These types of error are hard to find as the compiler
doesn’t point to the line at which the error occurs.

● warning: division by zero [-Wdiv-by-zero] div = n/0;


Linker Errors:
● These error occurs when after compilation we link the
different object files with main’s object
using Ctrl+F9 key(RUN).
● These are errors generated when the executable of the
program cannot be generated.
● This may be due to wrong function prototyping, incorrect
header files.
Logical Errors :

● On compilation and execution of a program, desired


output is not obtained when certain input values are
given.
● These types of errors which provide incorrect output
but appears to be error free are called logical
errors.
● These are one of the most common errors done by
beginners of programming.
Semantic errors : This error occurs when the statements
written in the program are not meaningful to the
compiler.
Algorithm analysis
● Analysing an algorithm means solving a problem
statement of an algorithm with different
approaches.

● It also means to check for the best algorithm.


Algorithm analysis
Analysis of an algorithm performed in two phases:
1. Prior estimate: It is concerned with
theoretical and mathematical calculation that
uses asymptotic notations for its representation.

2. Posterior estimate: It is concerned with


writing a program and therefore depends on
operating system, compiler, etc.
Algorithm analysis
● Algorithm analysis is determination of time,
amount of memory utilized and other resources by
the system to execute a particular task.

● The running time of an algorithm for particular


input based upon the number of operations executed
on that particular size of the input.

● The greater the number of operations performed,


the longer the running time of an algorithm.
Algorithm analysis
● Consider the input size denoted as n, and then the running time of that
problem is function f(n).
● Running time of an algorithm measured using following steps:
1. Computing primary functions: Primary functions are instructions with
constant implementation time.
(a) Assignment of variables.
(b) Performing arithmetic operations like addition of numbers,
subtraction of numbers, etc.
(c) Comparing two numbers.
(d) Defining a function and calling that function.
2. Computing operation of input size n: Growth rate represented in algorithm
as function f(n).
Comparing algorithms
● Algorithms are compared based on its time and space
complexity.
● Algorithms with less time complexity and less memory space
is said to be a better algorithm.
Performance analysis of an algorithm depends on two factors:
1. Space complexity: The amount of memory space required
by an algorithm to complete its execution.

2. Time complexity: The amount of time required by an


algorithm to complete its execution. Number of step
count taken by algorithm to complete its task.
Comparing algorithms
● There are three types of step count:
(a) Best case: The best-case step count is the minimum
number of steps executed for given parameters.
(b) Worst case: The worst-case step count is the
maximum number of steps executed for given
parameters.
(c) Average case: The average case step count is the
average number of steps executed for given
parameters
There are three different
Asymptotic notations: big O, big Theta
Notation
Asymptotic Notation is used
(Θ), and big Omega (Ω).

to describe the running time •Big-Θ is used when the


of an algorithm - how much running time is the same for
time an algorithm takes all cases,
with a given input, n. •Big-O for the worst case
running time
•Big-Ω for the best case
running time.
What is Time Complexity?
Time Complexity tells us how long it takes for an algorithm to run as the size of the input grows. Think of it like this: if you have a list of numbers and you want to find a specific
number, the time complexity will tell you how many steps it takes to find it.
Example: Imagine you have a list of 5 numbers: [2, 4, 6, 8, 10], and you want to find the number 8.
•Best Case: You might be lucky, and the number is the first one on the list. This is the quickest case.
•Worst Case: The number could be the last one, or not in the list at all, so you have to check every number.
What is Space Complexity?
Space Complexity is about how much memory or space your algorithm needs to run. For example, if you're solving a math problem and you need to write down some notes, the number
of pages you use is like the space complexity.
Example: If you are adding up the numbers in a list, you might only need one piece of paper to keep track of the total. So, the space complexity is small.
Putting It Together:
Imagine you’re searching for a toy in a toy box:
1.Time Complexity: How many toys do you have to pull out before you find the one you're looking for? If there are 10 toys, you might have to look at all 10.
2.Space Complexity: How much space do you need to keep track of the toys? If you’re only looking for one toy, you don’t need much space.
Simple Example in Plain English:
Let’s say you have a deck of cards, and you want to find the Ace of Spades:
•Time Complexity: If you have to go through every card one by one, it might take you 52 steps. This is an example of O(n), where n is the number of cards.
•Space Complexity: If you don’t need to write anything down or keep track of anything other than the card you're looking at, the space complexity is O(1), meaning you only need a tiny
bit of space.
In Summary:
•Time Complexity is about how long it takes to do something.
•Space Complexity is about how much space or memory you need.
Explore best-case, worst-case, and average-case scenarios using a simple example: searching for a number in a list
(or array) of numbers.
Example Scenario: Linear Search
Suppose you have a list of numbers, and you want to find a specific number. Linear search is an algorithm where you
check each number in the list one by one until you find the one you're looking for.
[3, 7, 1, 9, 5]
Let’s say you want to find the number 9 in this list.
Best Case:
What is it?
The best-case scenario happens when the number you’re looking for is the first one in the list.
Why is it best?
You find the number immediately, so you only have to check one number.
Example: If you were searching for 3, the algorithm would find it on the first try.
Time Complexity: The best-case time complexity is O(1), meaning it takes a constant amount of time regardless of
the size of the list.
Worst Case:
What is it?
The worst-case scenario occurs when the number you’re searching for is the last one in the list or isn’t in the list at all.

Why is it worst?
You have to check every single number in the list before you either find it or realize it’s not there.

Example:
If you were searching for 5, you would have to look at every number in the list: [3, 7, 1, 9] before finding it.
If you were searching for a number that isn't in the list (e.g., 8), you would also have to look at every number before concluding it's not
there.

Time Complexity:
The worst-case time complexity is O(n), where n is the number of items in the list.

Average Case:
What is it?
The average-case scenario is what typically happens. It’s an average of all the possible cases.

Why is it average?
It represents the typical performance of the algorithm when you don’t know where the target number is.

Example:
If you were searching for 7, on average, you'd expect to look through about half the list before finding the number.

Time Complexity:
The average-case time complexity is also O(n) for linear search, because, on average, you may have to look through about half of the list.
Summary of Cases in Linear Search:
Best Case: 𝑂(1) – You find the number right away.
Worst Case: 𝑂(𝑛) – You have to look at every number.
Average Case: 𝑂(𝑛) – On average, you check about half the numbers.

List: [3, 7, 1, 9, 5]
Best Case: Searching for 3 → Found on the 1st try (O(1)) –
Average Case: Searching for 7 → Found on the 2nd or 3rd try on average (O(n)) –
Worst Case: Searching for 5 or a number not in the list → Found on the 5th try, or not at all (O(n))
def swap(a, b):
temp = a
a=b
b = temp

What Does This Program Do?


This program takes two variables, a and b, and swaps their values. After running the function, a will have the value of b, and b will have the value of a.
Step-by-Step Analysis of Time Complexity
Time Complexity measures how long it takes to run the program
Step 1: temp = a
The program creates a temporary variable temp and assigns it the value of a.
How long does this step take?
This is a simple assignment operation, which takes a constant amount of time.
Time Complexity for this step: 𝑂(1)
Step 2: a = b
The program now assigns the value of b to a.
How long does this step take?
This is another simple assignment operation, taking a constant amount of time.
Time Complexity for this step: 𝑂(1)
Step 3: b = temp
Finally, the program assigns the value stored in temp (which was originally a) to b.
How long does this step take?
This is also a simple assignment operation, taking a constant amount of time.
Time Complexity for this step: 𝑂(1)
Overall Time Complexity
Since each step takes a constant amount of time, and there are three steps, the total time complexity is O(1).
What does this mean?
The time it takes to run the program does not depend on the size of the input. It always takes the same amount of
time, no matter what values a and b have.
Step-by-Step Analysis of Space Complexity
Space Complexity measures how much memory the program needs to run.
Step 1: Variables a and b
The program starts with two variables, a and b.
These are already given to the program, so they don’t count toward additional space.
Step 2: Creating temp
The program creates one new variable, temp, to temporarily store the value of a.
How much space does this take?
temp is just a single variable, so it uses a small, fixed amount of memory.
Space Complexity for this step: 𝑂(1)
Overall Space Complexity
The program only creates one extra variable, temp, so the space complexity is O(1).
What does this mean?
The memory required for the program does not change, no matter what values a and b have. The program always needs a fixed,
small amount of memory.
Time Complexity: 𝑂(1) – The program takes a constant amount of time to run, no matter what the values of a and b
Space Complexity: 𝑂(1) – The program uses a constant amount of memory, no matter what the values of a and b are.
Look at another simple example for 𝑂(log𝑛) time complexity:
Repeated Division by 2.
Example: Repeated Division by 2This program divides a number by 2 repeatedly until it becomes 1.

def divide_by_two(n):
while n > 1:
n = n // 2

Step-by-Step Execution
Let's say n = 16:
First Iteration:
n = 16
After dividing by 2, n = 8
Second Iteration:
n=8
After dividing by 2, n = 4
Third Iteration:
n=4
After dividing by 2, n = 2
Fourth Iteration:
n=2
After dividing by 2, n = 1
The loop stops when n becomes 1.
Time Complexity Calculation
Key Idea:
At each step, we divide the number by 2. This reduces the size of n by half.
The number of times we can divide n by 2 before it becomes 1 is proportional to the logarithm of n.
How Many Steps?:
Starting with n = 16, after 1 division: n = 8, after 2 divisions: n = 4, and so on. This halving continues until n = 1.
The number of iterations is approximately log2(𝑛). For example, if n = 16, it takes 4 iterations because log2(16)=4.
Conclusion:
The number of times the loop runs is proportional to log 𝑛, so the time complexity is 𝑂(log𝑛).
Space Complexity Calculation
Explanation:
The program uses only one variable (n), and no additional memory is used regardless of the input size.
Conclusion:
The space complexity is constant, or 𝑂(1).
Recap for 𝑂(log𝑛):
Time Complexity: 𝑂(log𝑛) because the number is halved repeatedly, reducing the number of steps logarithmically.
Space Complexity: 𝑂(1)because we only use a single variable, so no additional memory is required.
O(n)
for i in range(len(a)):
if a[i] == k:
print()
What Does This Program Do?
This program checks each element in the input array a to see if it is equal to a key value k. If it finds a match, it prints something (though the print statement is currently empty).
Step-by-Step Analysis of Time Complexity
Time Complexity measures how long the program takes to run, based on the size of the input.
Step 1: for i in range(len(a))
The loop runs from i = 0 to i = len(a) - 1.
How many times does the loop run?
The loop runs n times, where n is the length of the array a.
Time Complexity for this step: 𝑂(𝑛)
Step 2: if a[i] == k
For each iteration of the loop, the program checks whether the current element a[i] is equal to the key k.
How long does this check take?
This comparison is a simple operation, which takes a constant amount of time.
Time Complexity for this step: 𝑂(1)per comparison
Step 3: print()
If the condition a[i] == k is true, the program executes the print() statement.
How long does this step take?
The print() function itself takes a constant amount of time, but it’s only executed when the condition is met.
Time Complexity for this step: 𝑂(1) per execution
Overall Time Complexity
The loop runs 𝑛n times, and for each iteration, the comparison and possible print statement are executed.
Since each of these operations inside the loop takes constant time, the overall time complexity is dominated by the loop, which runs 𝑛
times.
Total Time Complexity: 𝑂(𝑛)
This means the time it takes to run the program grows linearly with the size of the array a.
Step-by-Step Analysis of Space Complexity
Space Complexity measures how much memory the program needs to run.
Step 1: Variables Used
The program uses:
The input array a.
The key k.
The loop variable i.
Step 2: Memory Used by Variables
The array a and key k are inputs and don't add to the space complexity.
The loop variable i is a single integer, using a small, fixed amount of memory.
The print() function does not add to the space complexity, as it does not store anything in memory .
Overall Space Complexity

The space complexity is O(1) (constant space) because the program only uses a fixed amount of additional memory (for

the loop variable i and the comparison).

Total Space Complexity: 𝑂(1)

This means the memory required for the program does not change with the size of the input array a.

Recap:

Time Complexity: 𝑂(𝑛) – The time to run the program increases linearly with the size of the array a.

Space Complexity: 𝑂(1) – The memory required by the program remains constant, regardless of the size of the array a.
example to explain 𝑂(𝑛 log𝑛) time complexity in a way that's def double_and_count(apples):

easy to understand. if len(apples) <= 1: # Base case: if we have 1 or fewer

Example: Doubling and Counting apples, stop

Imagine you have n items (let’s say apples), and you need to return apples

double the number of apples and count them at the same


time. Here's the approach: mid = len(apples) // 2 # Step 1: Find the middle point

Step 1: Start with the total number of apples. left_half = apples[:mid] # Step 2: Divide the apples into

Step 2: Divide the apples into two halves. two halves

Step 3: Double the apples in each half. right_half = apples[mid:]

Step 4: Count how many apples you have after doubling.


Step 5: Repeat until you've doubled all apples. # Step 3: Double the apples in each half (recursively)

The doubling process happens multiple times and each time left_doubled = double_and_count(left_half)

you're working on fewer apples. right_doubled = double_and_count(right_half)

return left_doubled + right_doubled # Combine both


halves
Time Complexity Calculation (in Simple Terms)
Step 1: You start by dividing the number of apples into two halves. For each half, you double the apples.
Step 2: Every time you divide the apples, you do it until you reach a single apple.
The number of divisions is related to the logarithm of the number of apples, so this is the log 𝑛 part.
Step 3: For each division (each step), you are counting all the apples, which takes 𝑛 steps because you go through all
the apples.
The counting process takes 𝑛n steps for every division, which makes it linear in nature, or 𝑂(𝑛).
So, since you’re dividing and counting, you get 𝑂(𝑛 log𝑛)as the total time complexity.
Space Complexity Calculation
Explanation: Each time you divide the apples, you store the halves separately. You do this until you have individual
apples, so you use space proportional to the number of apples.
Conclusion: The space complexity is 𝑂(𝑛).
Recap for 𝑂(𝑛 log𝑛):
Time Complexity: 𝑂(𝑛 log𝑛) because you are dividing (logarithmic part) and counting (linear part) at each step.
Space Complexity: 𝑂(𝑛) because you are using extra space to store the divided apples.
the concept of O(n^2) with a very basic example that involves nested loops—the classic case for quadratic time complexity.
Example: Simple Nested Loop
Imagine you are asked to print every pair of students in your class. If there are 5 students, you want to print all possible pairs (student 1 with
student 2, student 1 with student 3, and so on).
Here's a simple Python program that prints all pairs:
def print_pairs(students):
for i in range(len(students)): # Outer loop runs n times
for j in range(len(students)): # Inner loop runs n times
print(students[i], students[j]) # Printing each pair
Example: If students = ["Alice", "Bob", "Charlie"], the program will print:
Alice Alice
Alice Bob
Alice Charlie
Bob Alice
Bob Bob
Bob Charlie
Charlie Alice
Charlie Bob
Charlie Charlie
Time Complexity Calculation
Outer Loop:
The outer loop runs once for each student. So, if there are n students, this loop runs n times.
Inner Loop:
For each iteration of the outer loop, the inner loop also runs n times (because you're printing pairs for each student).
Total Steps:
For each student in the outer loop, you're running the inner loop n times. So, if the outer loop runs n times and for each time
the inner loop also runs n times, you have 𝑛×𝑛=𝑛^2 steps.
Conclusion: The time complexity is 𝑂(𝑛^2) because the number of operations increases quadratically as the number of
students (n) increases.
Space Complexity Calculation
Explanation:
We aren't using any extra space except for the list of students, which is n. The nested loops don't create any additional data
structures, so the space used is proportional to the size of the input list.
Conclusion: The space complexity is 𝑂(𝑛) because the memory used is proportional to the number of students, but no extra
data structures are created.
Explain O(2^n) time complexity with a simple example that involves recursion—a classic case where the number of
operations grows exponentially as the input size increases.
Example: Recursive Fibonacci Sequence
The Fibonacci sequence is a famous example of O(2^n) time complexity. The Fibonacci sequence is defined as:
•F(0)=0
•F(1)=1
•F(n)=F(n−1)+F(n−2) for n≥2n
Here's a simple recursive Python program to calculate Fibonacci numbers:
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
How the Fibonacci Program Works
Let’s say you want to find fibonacci(4):
To calculate fibonacci(4), the function needs to calculate:
fibonacci(3) and fibonacci(2)
For fibonacci(3), the function will calculate:
fibonacci(2) and fibonacci(1)
This process continues recursively until the base cases (n = 0 or n = 1) are reached.
Time Complexity Calculation
Recursive Calls:
For each fibonacci(n) call, the function calls itself twice: once for fibonacci(n-1) and once for fibonacci(n-2).
Tree-like Structure:
This forms a tree-like structure of recursive calls. At each level of the tree, the number of recursive calls doubles.
Number of Calls:
For fibonacci(n), there are approximately 2^𝑛 function calls, since each call branches out into two more calls.
Conclusion: The number of function calls grows exponentially, so the time complexity is 𝑂(2^𝑛).
Space Complexity Calculation

Explanation:

Each recursive call uses up space on the call stack. Since the function calls itself twice at each step, the maximum depth

of the recursion is proportional to n.

Conclusion: The space complexity is linear, or 𝑂(𝑛), because at any point, the maximum depth of recursion will be n.
124
Big-O Notation (O-notation) – Worst Case-Upper Limit
125
Omega Notation (Ω-notation) – Lower Bound-Best Case
126

Theta Notation (Θ-notation) – Upper & Lower Bound


127
Little o (o) and Little Omega (ω) – Loose Upper & Lower Bounds
Thanks!

Do you have
any
questions?

You might also like