SlideShare a Scribd company logo
Data Structures And Algorithms
Text Books
• Suggested Textbooks:
1. Fundamentals of Data Structures, E.Horowitz and
S.Sahni,1977
2. Data Structures and Algorithms, alfred V.Aho, John
E.Hooperoft, Jeffrey D.Ullman
COURSE OBJECTIVES
1. To impart the basic concepts of data structures and algorithms.
2. To introduce various searching and sorting techniques.
3. To demonstrate operations of linear and non-linear data structure.
4. To develop an application using suitable data structure.
COURSE OUTCOMES
After completion of the course, the student will be able to
1. Understand the specifications for writing algorithms and analyze its
performance.
2. Analyze basic concepts of data structures, computation complexity.
3. Understand linear data structures, various sorting, searching
techniques.
4. Apply various operations on linear and non-linear data structures.
5. Identify appropriate and efficient data structure to implement a
given problem.
UNIT-1
Basic Terminologies & Introduction to algorithm and Data
Organization:
• Algorithm Specification
• Recursion
• Performance Analysis
• Asymptotic Notation
• The Big-O, Omega and Theta notation
• Programming style
• Refinement of coding-time-space trade off
• Testing and Data abstraction
What is an algorithm?
An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of
time.
“computer”
problem
algorithm
input output
Algorithmic solution : a way to teach the computer
Notion of algorithm
An Algorithm must satisfy the following criteria
(features).
1) Input
2) Output
3) Definiteness
4) Finiteness
5) Effectiveness
1)Input- Zero or more quantities externally supplied.
2)Output- At least one quantity is produced.
3)Definiteness- Each instruction is clear and
unambiguous.
4)Finiteness- If we trace out the instructions of an
algorithm, then for all cases the algorithm terminates
after a finite number of steps.
5)Effectiveness- Every step of algorithm should be
feasible.
Basic Issues Related to Algorithms
• How to design algorithms
– Understanding the problem (instances, ranges)
– Ascertaining the capabilities of a computational device
(RAM and parallel machines, speed and amount of memory)
– Choosing between exact and approximate algorithms
– Deciding the correct data structure
– Algorithm design techniques
• How to express algorithms (Specification)
– Methods of specifying an algorithm (free language, pseudo
code, flowchart)
Basic Issues Related to Algorithms
• Proving correctness (Verification)
– Algorithm’s Proof
• Analyzing an algorithm (Analysis)
Efficiency, theoretical and empirical analysis, optimality
• Coding an algorithm (Implementation)
Algorithm Design Techniques/Strategies
 Greedy Approach
 Dynamic Programming
 Back Tracking
 Branch and Bound
 Brute Force Technique
 Divide and Conquer
New Microsoft Word Document.docx
Pseudo code for expressing Algorithms
Algorithm is broadly divided into 2 sections.
1. Algorithm heading- It consists of name of algorithm,
problem description, input and output.
Ex:- Algorithm name(v1,v2,……,vn)
In heading section we should write the following things:-
//Problem Description:
//Input:
//Output:
2. Algorithm body- It consists of logical body of the
program by making use of various programming constructs
and assignment statements.
• The Body of an algorithm is written, in which various
programming constructs like if, for, while and some
assignment statements.
• The compound statements should be enclosed with in {
and } brackets.
• Single line comments are written using // as beginning of
comment.
• The Identifier should begin by letter and not by digit. An
identifier can be a combination of alphanumeric string.
• Assignment operator 
Ex:- Variable  Expression
• There are other types of operators like Boolean(true , false),
Logical(and , or , not) and Relational(<,<=,>,>=,=).
• Array indices [ , ] initial index is 0.
• Inputting and outputting can be done using read and write.
Ex:- write(“WELCOME”);
read(val);
• The conditional statements if-then and if-then-else.
If(condition) then statement
If(condition) then statement else statement
We can use brackets also { and }.
• While statement
While(condition) do
{ Statement 1;
Statement 2;
….…
Statement n;
}
• For statement
For variable  value1 to valuen do (step i)
{
Statement 1
Statement 2
……..
Statement n
}
• Repeat-Until statement
repeat
Statement 1
Statement 2
……..
Statement n
until(condition)
• Break statement is used to exit from inner loop.
• Return statement is used to return control from one
point to another.
Examples:-
1. Write an algorithm to find sum of n numbers.
2. Write an algorithm to check the given number is
even or odd.
3. Write an algorithm to find factorial of a number.
4. Write an algorithm to sort the given list of
elements.
Algorithm sum()
// Prob Desc: This alg is used to find sum of n numbers
// input: n value
// output: sum of n numbers
{
Write(“enter n”);
Read(n);
i0;
Repeat
{
sumsum+I;
ii+1;
}
Until(i<=n)
Write(“sum of n numbers is”);
Write(sum);
}
Performance Analysis
• The efficiency of an algorithm can be decided by
measuring the performance of an algorithm. we can
measure the performance of an algorithm by
computing two factors.
1. Amount of TIME required by an algorithm to
execute. (Time Complexity)
2. Amount of STORAGE required by an algorithm.
(Space Complexity)
Space Complexity
• The Space Complexity can be defined as amount of memory
required by an algorithm to run.
• To compute Space Complexity we use two factors:-
• Constant and Instance Characteristics.
• The space requirement S(p) can be given as:
S(p) = C + Sp
Where C is constant i.e fixed part it denotes the space of inputs
and outputs.
This space is an amount of space taken by instruction, variables
and identifiers.
• Sp is a space depends upon instance characteristics.
• This is a variable part whose space requirement depends on
particular problem instance.
Ex:- Addition of two numbers.
S(p) = C Ө(Sp) = 0
Ex:- Addition of array elements.
S(p) >= (n+3)
Ex:- Addition of array elements.(Recursive)
S(p) >= 3(n+1)
The internal stack used for recursion includes space for formal
parameters, local variables and return address.
Time Complexity
• The time complexity of an algorithm is the amount of
computer time required by an algorithm to run to completion.
• Executing time depends on many factors:-
 System load
 Number of other programs running
 Instruction set used
 Speed of underlying hardware
• Therefore the time complexity is in terms of frequency count.
• Frequency count is a count denoting number of times of
execution of a statement.
• Measuring an input size
• Measuring running time
T(n)= C(n) Cop
- Where T(n) is running time of basic operation
- Cop is Time taken by the basic operation to execute
- C(n) is number of times the operation needs to be
executed
• Order of Growth:- Measuring the performance of an
algorithm in relation with the input size n is called Order
of Growth.
• Example the order of growth for varying input
size of n
logn n nlogn n*n POW(2,n)
0 1 0 1 2
1 2 2 4 4
2 4 8 16 16
3 8 24 64 256
4 16 64 256 65,536
5 32 160 1024 4,294,967,296
Asymptotic Notations
• To choose the best algorithm, we need to check efficiency of
each algorithm.
• The efficiency can be measured by computing time
complexity of each algorithm.
• Asymptotic notation is a shorthand way to represent the time
complexity.
• Using asymptotic notations we can give time complexity as
“Fastest possible”, “Slowest Possible” or “Average Time”.
• Various notations such as ,ϴ and  used are called
Asymptotic notations.
1.Big oh Notation
• The Big oh notation is denoted by “”.
• It is a method of representing the upper bound of algorithms
running time.
• Using Big oh notation we can give longest amount of time taken by
the algorithm to complete.
• Definition:- f(n)=O(g(n))
Let f(n) and g(n) be two non negative functions.
Let n0 and constant c are two integers such that no denotes some
value of input and n>=n0
similarly c is some constant such that c>0
we can write F(n)<=c*g(n) for all n>=n0
• Then F(n) is Big oh of g(n). It is also denoted as F(n) Є
O(g(n)). In other words F(n) is <=g(n) is multiple of
some constant c.
• Ex:- Consider the function F(n)=2n+2 and g(n)=n2
then we have to find some constant c so that
f(n)<=c*g(n).
if n=1 then f(n) > g(n)
if n=2 then f(n) > g(n)
if n=3 then f(n) < g(n)
• Thus always upper bound of existing time is obtained
by Big oh notation.
2.Omega Notation
• Omega notation is denoted by “”.
• This notation is used to represent the Lower Bound of
algorithm’s running time.
• Using Omega notation we can denote shortest amount of
time taken by algorithm.
Definition:-
• A function f(n) is said to be in (g(n)) if f(n) is bounded below
by some positive constant multiple of g(n) such that
• F(n) > = c*g(n) for all n>=n0
• It is denoted as f(n) Є (g(n))
• Ex:- Consider f(n)=2n2+5 and g(n)=7n
n=0 ,1,2 and 3
3.Theta Notation
• The theta notation is denoted by “ϴ”.
• By this method the running time is between upper bound and
lower bound.
• Definition:-
• Let f(n) and g(n) be two non negative functions. There are two
positive constants namely C1 and C2 such that
C1*g(n)<=f(n)<=C2*g(n) for all n, n>=n0
f(n) Є ϴ (g(n))
Ex:- f(n)=2n+8 and g(n)=7n where n>=2
Similarly f(n)=2n+8 and g(n)=7n
i.e 5n < 2n+8 < 7n for n>=2
Performance Analysis
1. O(1): Time complexity of a function (or set of statements) is
considered as O(1) if it doesn’t contain loop, recursion and
call to any other non-constant time function.
2. O(n): Time Complexity of a loop is considered as O(n) if the
loop variables is incremented / decremented by a constant
amount.
3. O(Logn):Time Complexity of a loop is considered as O(Logn) if
the loop variables is divided / multiplied by a constant
amount.
4) O(nc): Time complexity of nested loops is equal to the
number of times the innermost statement is executed.
For example the algorithm addition of two matrices
contains two loops then we have O(n2) time complexity.
5) O(LogLogn): Time Complexity of a loop is considered as
O(LogLogn) if the loop variables is reduced / increased
exponentially by a constant amount.
Analysis of Algorithms
• We can have three cases to analyze an algorithm:
1) Worst Case
2) Average Case
3) Best Case
Ex:- Linear Search
int search(int arr[], int n, int x)
{
int i;
for (i=0; i<n; i++)
{
if (arr[i] == x)
return i;
}
return -1;
}
• If we use the ϴ notation for time complexity then
• Worst Case is ϴ(n).
• Average Case is
∑(i=1 to n+1)(ϴ(i))
-----------------------
n+1
• i.e Θ(n)
• Best Case is ϴ(1)
Binary Search
• Worst Case O(n) for an unsorted array and O(logn)
for sorted array.
• Best Case is O(1) (if the key element is equal to the
first element of the given array).
• Average Case O(logn)
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
Big oh Analysis of Algorithms
Time Complexities of all Sorting Algorithms
Best Average Worst
Selection Sort Ω(n^2) θ(n^2) O(n^2)
Bubble Sort Ω(n) θ(n^2) O(n^2)
Insertion Sort Ω(n) θ(n^2) O(n^2)
Heap Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
Quick Sort Ω(n log(n)) θ(n log(n)) O(n^2)
Merge Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
• Programming Style
1. Clarity and simplicity of Expression: The programs should be
designed in such a manner so that the objectives of the program is
clear.
2. Naming: In a program, you are required to name the module,
processes, and variable, and so on. Care should be taken that the
naming style should not be cryptic and non-representative.
For Example: a = 3.14 * r * r
area_of_circle = 3.14 * radius * radius;
• Programming Style
3. Spaces: Style related to white space is commonly used to enhance
readability. For instance, compare the following syntactically
equivalent examples of C code:
int i;
for(i=0;i<10;++i){
printf("%d",i*i+i);
}
versus
int i;
for (i = 0; i < 10; ++i) {
printf("%d", i * i + i);
}
• Programming Style
4. Tabs: tabs work fine provided they are used consistently, restricted to
logical indentation, and not used for alignment.
int ix;
long sum;
5. Control Constructs: It is desirable that as much as a possible single entry
and single exit constructs used.
6. Indentation: Indentation styles assist in identifying control flow and
blocks of code. In some programming languages, indentation is used to
delimit logical blocks of code; correct indentation in these cases is more
than a matter of style. In other languages, indentation and white space do
not affect function, although logical and consistent indentation makes code
more readable.
7. Nesting: Deep nesting of loops and conditions greatly harm the
static and dynamic behaviour of a program. It also becomes difficult
to understand the program logic, so it is desirable to avoid deep
nesting.
8. Module size: The module size should be uniform. The size of the
module should not be too big or too small.
Refinement of coding-time-Space trade off
DSA
DSA
DSA
Testing:-
• Technically Testing is a process to check if the application is
working same as it was supposed to do, and not working as it was
not supposed to do.
• Main objective of Testing is to find bugs and errors in an
application which get missed during the unit testing by the
developer.
• A unit is the smallest testable part of any software. It usually has
one or a few inputs and usually a single output.
• A unit may be an individual program, function, procedure, etc
• Benefits:
• Unit testing increases confidence in changing / maintaining code.
If good unit tests are written and if they are run every time any
code is changed, we will be able to promptly catch any defects
introduced due to the change. Also, if codes are already made less
interdependent to make unit testing possible, the unintended
impact of changes to any code is less.
• Codes are more reusable. In order to make unit testing possible,
codes need to be modular. This means that codes are easier to
reuse.
• Debugging is easy. When a test fails, only the latest changes need
to be debugged.
• Data abstraction
• Data Abstraction is the property by virtue of which only the
essential details are displayed to the user.
• The trivial or the non-essentials units are not displayed to the user.
Ex: A car is viewed as a car rather than its individual components.
• Data Abstraction may also be defined as the process of identifying
only the required characteristics of an object ignoring the
irrelevant details.
• The properties and behaviors of an object differentiate it from
other objects of similar type and also help in classifying/grouping
the objects.
• Consider a real-life example of a man driving a car.
• The man only knows that pressing the accelerators will increase
the speed of car or applying brakes will stop the car, but he does
not know about how on pressing the accelerator the speed is
actually increasing, he does not know about the inner mechanism
of the car or the implementation of the accelerator, brakes, etc in
the car. This is what abstraction is.
• Advantages of Abstraction
• It reduces the complexity of viewing the things.
• Avoids code duplication and increases reusability.
• Helps to increase security of an application or program as only
important details are provided to the user.

More Related Content

PPTX
Translation of expression(copmiler construction)
PPTX
Directed Acyclic Graph Representation of basic blocks
PPTX
Algorithm Introduction
PPT
Transport services
DOC
Time and space complexity
PPTX
Time space trade off
PPTX
asymptotic analysis and insertion sort analysis
PPT
Divide and conquer
Translation of expression(copmiler construction)
Directed Acyclic Graph Representation of basic blocks
Algorithm Introduction
Transport services
Time and space complexity
Time space trade off
asymptotic analysis and insertion sort analysis
Divide and conquer

What's hot (20)

PPTX
Reference model of real time system
PPTX
Software Project Management - Staffing
PPTX
Internet of Things: Research Directions
PPT
Swap-space Management
PPTX
Pipelining powerpoint presentation
PPTX
Performance analysis and randamized agoritham
PPT
Real-Time Scheduling
PPTX
Fuzzy Sets Introduction With Example
PPT
Pipeline hazards in computer Architecture ppt
PPT
Z transfrm ppt
PPT
Greedymethod
PDF
Gradient descent method
PPT
Specification and complexity - algorithm
PPT
Asymptotic Notation and Complexity
PPT
Introduction and architecture of expert system
PPTX
Register allocation and assignment
PPTX
system properties
PPTX
Memory Management in OS
PDF
Finite fields
PPT
K mean-clustering algorithm
Reference model of real time system
Software Project Management - Staffing
Internet of Things: Research Directions
Swap-space Management
Pipelining powerpoint presentation
Performance analysis and randamized agoritham
Real-Time Scheduling
Fuzzy Sets Introduction With Example
Pipeline hazards in computer Architecture ppt
Z transfrm ppt
Greedymethod
Gradient descent method
Specification and complexity - algorithm
Asymptotic Notation and Complexity
Introduction and architecture of expert system
Register allocation and assignment
system properties
Memory Management in OS
Finite fields
K mean-clustering algorithm
Ad

Similar to DSA (20)

PPTX
02 Introduction to Data Structures & Algorithms.pptx
PPTX
2. Introduction to Algorithm.pptx
PPTX
Unit ii algorithm
PPTX
Unit 1, ADA.pptx
PPTX
Algorithms & Complexity Calculation
PPTX
Algorithm.pptx
PPTX
Algorithm.pptx
PPTX
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
PPTX
Searching Algorithms
PPTX
Data Structures and Algorithms for placements
PPTX
DAA-Unit1.pptx
PPTX
Design Analysis of Alogorithm 1 ppt 2024.pptx
PPTX
Analysis of Algorithm full version 2024.pptx
PPTX
Modile-1-PPT-1-BCAC0207-AlgorithmDesign.pptx
PPTX
Algorithm for the DAA agscsnak javausmagagah
PPTX
L1_Start_of_Learning_of_Algorithms_Basics.pptx
PDF
introduction of Data structure with example
PPTX
Unit 1.pptx
PPTX
L1_DatabAlgorithm Basics with Design & Analysis.pptx
PPTX
ANALYSIS AND DESIGN OF ALGORITHMS -M1-PPT
02 Introduction to Data Structures & Algorithms.pptx
2. Introduction to Algorithm.pptx
Unit ii algorithm
Unit 1, ADA.pptx
Algorithms & Complexity Calculation
Algorithm.pptx
Algorithm.pptx
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
Searching Algorithms
Data Structures and Algorithms for placements
DAA-Unit1.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
Analysis of Algorithm full version 2024.pptx
Modile-1-PPT-1-BCAC0207-AlgorithmDesign.pptx
Algorithm for the DAA agscsnak javausmagagah
L1_Start_of_Learning_of_Algorithms_Basics.pptx
introduction of Data structure with example
Unit 1.pptx
L1_DatabAlgorithm Basics with Design & Analysis.pptx
ANALYSIS AND DESIGN OF ALGORITHMS -M1-PPT
Ad

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Cell Structure & Organelles in detailed.
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Complications of Minimal Access Surgery at WLH
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Institutional Correction lecture only . . .
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Structure & Organelles in detailed.
VCE English Exam - Section C Student Revision Booklet
Complications of Minimal Access Surgery at WLH
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPH.pptx obstetrics and gynecology in nursing
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Pre independence Education in Inndia.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
human mycosis Human fungal infections are called human mycosis..pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

DSA

  • 1. Data Structures And Algorithms
  • 2. Text Books • Suggested Textbooks: 1. Fundamentals of Data Structures, E.Horowitz and S.Sahni,1977 2. Data Structures and Algorithms, alfred V.Aho, John E.Hooperoft, Jeffrey D.Ullman
  • 3. COURSE OBJECTIVES 1. To impart the basic concepts of data structures and algorithms. 2. To introduce various searching and sorting techniques. 3. To demonstrate operations of linear and non-linear data structure. 4. To develop an application using suitable data structure.
  • 4. COURSE OUTCOMES After completion of the course, the student will be able to 1. Understand the specifications for writing algorithms and analyze its performance. 2. Analyze basic concepts of data structures, computation complexity. 3. Understand linear data structures, various sorting, searching techniques. 4. Apply various operations on linear and non-linear data structures. 5. Identify appropriate and efficient data structure to implement a given problem.
  • 5. UNIT-1 Basic Terminologies & Introduction to algorithm and Data Organization: • Algorithm Specification • Recursion • Performance Analysis • Asymptotic Notation • The Big-O, Omega and Theta notation • Programming style • Refinement of coding-time-space trade off • Testing and Data abstraction
  • 6. What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. “computer” problem algorithm input output Algorithmic solution : a way to teach the computer Notion of algorithm
  • 7. An Algorithm must satisfy the following criteria (features). 1) Input 2) Output 3) Definiteness 4) Finiteness 5) Effectiveness
  • 8. 1)Input- Zero or more quantities externally supplied. 2)Output- At least one quantity is produced. 3)Definiteness- Each instruction is clear and unambiguous. 4)Finiteness- If we trace out the instructions of an algorithm, then for all cases the algorithm terminates after a finite number of steps. 5)Effectiveness- Every step of algorithm should be feasible.
  • 9. Basic Issues Related to Algorithms • How to design algorithms – Understanding the problem (instances, ranges) – Ascertaining the capabilities of a computational device (RAM and parallel machines, speed and amount of memory) – Choosing between exact and approximate algorithms – Deciding the correct data structure – Algorithm design techniques • How to express algorithms (Specification) – Methods of specifying an algorithm (free language, pseudo code, flowchart)
  • 10. Basic Issues Related to Algorithms • Proving correctness (Verification) – Algorithm’s Proof • Analyzing an algorithm (Analysis) Efficiency, theoretical and empirical analysis, optimality • Coding an algorithm (Implementation)
  • 11. Algorithm Design Techniques/Strategies  Greedy Approach  Dynamic Programming  Back Tracking  Branch and Bound  Brute Force Technique  Divide and Conquer New Microsoft Word Document.docx
  • 12. Pseudo code for expressing Algorithms Algorithm is broadly divided into 2 sections. 1. Algorithm heading- It consists of name of algorithm, problem description, input and output. Ex:- Algorithm name(v1,v2,……,vn) In heading section we should write the following things:- //Problem Description: //Input: //Output:
  • 13. 2. Algorithm body- It consists of logical body of the program by making use of various programming constructs and assignment statements. • The Body of an algorithm is written, in which various programming constructs like if, for, while and some assignment statements. • The compound statements should be enclosed with in { and } brackets. • Single line comments are written using // as beginning of comment.
  • 14. • The Identifier should begin by letter and not by digit. An identifier can be a combination of alphanumeric string. • Assignment operator  Ex:- Variable  Expression • There are other types of operators like Boolean(true , false), Logical(and , or , not) and Relational(<,<=,>,>=,=). • Array indices [ , ] initial index is 0. • Inputting and outputting can be done using read and write. Ex:- write(“WELCOME”); read(val);
  • 15. • The conditional statements if-then and if-then-else. If(condition) then statement If(condition) then statement else statement We can use brackets also { and }. • While statement While(condition) do { Statement 1; Statement 2; ….… Statement n; }
  • 16. • For statement For variable  value1 to valuen do (step i) { Statement 1 Statement 2 …….. Statement n } • Repeat-Until statement repeat Statement 1 Statement 2 …….. Statement n until(condition)
  • 17. • Break statement is used to exit from inner loop. • Return statement is used to return control from one point to another. Examples:- 1. Write an algorithm to find sum of n numbers. 2. Write an algorithm to check the given number is even or odd. 3. Write an algorithm to find factorial of a number. 4. Write an algorithm to sort the given list of elements.
  • 18. Algorithm sum() // Prob Desc: This alg is used to find sum of n numbers // input: n value // output: sum of n numbers { Write(“enter n”); Read(n); i0; Repeat { sumsum+I; ii+1; } Until(i<=n) Write(“sum of n numbers is”); Write(sum); }
  • 19. Performance Analysis • The efficiency of an algorithm can be decided by measuring the performance of an algorithm. we can measure the performance of an algorithm by computing two factors. 1. Amount of TIME required by an algorithm to execute. (Time Complexity) 2. Amount of STORAGE required by an algorithm. (Space Complexity)
  • 20. Space Complexity • The Space Complexity can be defined as amount of memory required by an algorithm to run. • To compute Space Complexity we use two factors:- • Constant and Instance Characteristics. • The space requirement S(p) can be given as: S(p) = C + Sp Where C is constant i.e fixed part it denotes the space of inputs and outputs. This space is an amount of space taken by instruction, variables and identifiers.
  • 21. • Sp is a space depends upon instance characteristics. • This is a variable part whose space requirement depends on particular problem instance. Ex:- Addition of two numbers. S(p) = C Ө(Sp) = 0 Ex:- Addition of array elements. S(p) >= (n+3) Ex:- Addition of array elements.(Recursive) S(p) >= 3(n+1) The internal stack used for recursion includes space for formal parameters, local variables and return address.
  • 22. Time Complexity • The time complexity of an algorithm is the amount of computer time required by an algorithm to run to completion. • Executing time depends on many factors:-  System load  Number of other programs running  Instruction set used  Speed of underlying hardware • Therefore the time complexity is in terms of frequency count. • Frequency count is a count denoting number of times of execution of a statement.
  • 23. • Measuring an input size • Measuring running time T(n)= C(n) Cop - Where T(n) is running time of basic operation - Cop is Time taken by the basic operation to execute - C(n) is number of times the operation needs to be executed • Order of Growth:- Measuring the performance of an algorithm in relation with the input size n is called Order of Growth.
  • 24. • Example the order of growth for varying input size of n logn n nlogn n*n POW(2,n) 0 1 0 1 2 1 2 2 4 4 2 4 8 16 16 3 8 24 64 256 4 16 64 256 65,536 5 32 160 1024 4,294,967,296
  • 25. Asymptotic Notations • To choose the best algorithm, we need to check efficiency of each algorithm. • The efficiency can be measured by computing time complexity of each algorithm. • Asymptotic notation is a shorthand way to represent the time complexity. • Using asymptotic notations we can give time complexity as “Fastest possible”, “Slowest Possible” or “Average Time”. • Various notations such as ,ϴ and  used are called Asymptotic notations.
  • 26. 1.Big oh Notation • The Big oh notation is denoted by “”. • It is a method of representing the upper bound of algorithms running time. • Using Big oh notation we can give longest amount of time taken by the algorithm to complete. • Definition:- f(n)=O(g(n)) Let f(n) and g(n) be two non negative functions. Let n0 and constant c are two integers such that no denotes some value of input and n>=n0 similarly c is some constant such that c>0 we can write F(n)<=c*g(n) for all n>=n0
  • 27. • Then F(n) is Big oh of g(n). It is also denoted as F(n) Є O(g(n)). In other words F(n) is <=g(n) is multiple of some constant c.
  • 28. • Ex:- Consider the function F(n)=2n+2 and g(n)=n2 then we have to find some constant c so that f(n)<=c*g(n). if n=1 then f(n) > g(n) if n=2 then f(n) > g(n) if n=3 then f(n) < g(n) • Thus always upper bound of existing time is obtained by Big oh notation.
  • 29. 2.Omega Notation • Omega notation is denoted by “”. • This notation is used to represent the Lower Bound of algorithm’s running time. • Using Omega notation we can denote shortest amount of time taken by algorithm. Definition:- • A function f(n) is said to be in (g(n)) if f(n) is bounded below by some positive constant multiple of g(n) such that • F(n) > = c*g(n) for all n>=n0 • It is denoted as f(n) Є (g(n))
  • 30. • Ex:- Consider f(n)=2n2+5 and g(n)=7n n=0 ,1,2 and 3
  • 31. 3.Theta Notation • The theta notation is denoted by “ϴ”. • By this method the running time is between upper bound and lower bound. • Definition:- • Let f(n) and g(n) be two non negative functions. There are two positive constants namely C1 and C2 such that C1*g(n)<=f(n)<=C2*g(n) for all n, n>=n0 f(n) Є ϴ (g(n))
  • 32. Ex:- f(n)=2n+8 and g(n)=7n where n>=2 Similarly f(n)=2n+8 and g(n)=7n i.e 5n < 2n+8 < 7n for n>=2
  • 33. Performance Analysis 1. O(1): Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. 2. O(n): Time Complexity of a loop is considered as O(n) if the loop variables is incremented / decremented by a constant amount. 3. O(Logn):Time Complexity of a loop is considered as O(Logn) if the loop variables is divided / multiplied by a constant amount.
  • 34. 4) O(nc): Time complexity of nested loops is equal to the number of times the innermost statement is executed. For example the algorithm addition of two matrices contains two loops then we have O(n2) time complexity. 5) O(LogLogn): Time Complexity of a loop is considered as O(LogLogn) if the loop variables is reduced / increased exponentially by a constant amount.
  • 35. Analysis of Algorithms • We can have three cases to analyze an algorithm: 1) Worst Case 2) Average Case 3) Best Case Ex:- Linear Search int search(int arr[], int n, int x) { int i; for (i=0; i<n; i++) { if (arr[i] == x) return i; } return -1; }
  • 36. • If we use the ϴ notation for time complexity then • Worst Case is ϴ(n). • Average Case is ∑(i=1 to n+1)(ϴ(i)) ----------------------- n+1 • i.e Θ(n) • Best Case is ϴ(1)
  • 37. Binary Search • Worst Case O(n) for an unsorted array and O(logn) for sorted array. • Best Case is O(1) (if the key element is equal to the first element of the given array). • Average Case O(logn)
  • 38. int binarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l)/2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid-1, x); return binarySearch(arr, mid+1, r, x); } return -1; }
  • 39. Big oh Analysis of Algorithms
  • 40. Time Complexities of all Sorting Algorithms Best Average Worst Selection Sort Ω(n^2) θ(n^2) O(n^2) Bubble Sort Ω(n) θ(n^2) O(n^2) Insertion Sort Ω(n) θ(n^2) O(n^2) Heap Sort Ω(n log(n)) θ(n log(n)) O(n log(n)) Quick Sort Ω(n log(n)) θ(n log(n)) O(n^2) Merge Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
  • 41. • Programming Style 1. Clarity and simplicity of Expression: The programs should be designed in such a manner so that the objectives of the program is clear. 2. Naming: In a program, you are required to name the module, processes, and variable, and so on. Care should be taken that the naming style should not be cryptic and non-representative. For Example: a = 3.14 * r * r area_of_circle = 3.14 * radius * radius;
  • 42. • Programming Style 3. Spaces: Style related to white space is commonly used to enhance readability. For instance, compare the following syntactically equivalent examples of C code: int i; for(i=0;i<10;++i){ printf("%d",i*i+i); } versus int i; for (i = 0; i < 10; ++i) { printf("%d", i * i + i); }
  • 43. • Programming Style 4. Tabs: tabs work fine provided they are used consistently, restricted to logical indentation, and not used for alignment. int ix; long sum; 5. Control Constructs: It is desirable that as much as a possible single entry and single exit constructs used. 6. Indentation: Indentation styles assist in identifying control flow and blocks of code. In some programming languages, indentation is used to delimit logical blocks of code; correct indentation in these cases is more than a matter of style. In other languages, indentation and white space do not affect function, although logical and consistent indentation makes code more readable.
  • 44. 7. Nesting: Deep nesting of loops and conditions greatly harm the static and dynamic behaviour of a program. It also becomes difficult to understand the program logic, so it is desirable to avoid deep nesting. 8. Module size: The module size should be uniform. The size of the module should not be too big or too small.
  • 49. Testing:- • Technically Testing is a process to check if the application is working same as it was supposed to do, and not working as it was not supposed to do. • Main objective of Testing is to find bugs and errors in an application which get missed during the unit testing by the developer. • A unit is the smallest testable part of any software. It usually has one or a few inputs and usually a single output. • A unit may be an individual program, function, procedure, etc
  • 50. • Benefits: • Unit testing increases confidence in changing / maintaining code. If good unit tests are written and if they are run every time any code is changed, we will be able to promptly catch any defects introduced due to the change. Also, if codes are already made less interdependent to make unit testing possible, the unintended impact of changes to any code is less. • Codes are more reusable. In order to make unit testing possible, codes need to be modular. This means that codes are easier to reuse. • Debugging is easy. When a test fails, only the latest changes need to be debugged.
  • 51. • Data abstraction • Data Abstraction is the property by virtue of which only the essential details are displayed to the user. • The trivial or the non-essentials units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components. • Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. • The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.
  • 52. • Consider a real-life example of a man driving a car. • The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car, but he does not know about how on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of the accelerator, brakes, etc in the car. This is what abstraction is. • Advantages of Abstraction • It reduces the complexity of viewing the things. • Avoids code duplication and increases reusability. • Helps to increase security of an application or program as only important details are provided to the user.