DS-I - Introduction To Data Structure - 24 Oct 2018
DS-I - Introduction To Data Structure - 24 Oct 2018
Introduction to Algorithms
Atomic Data
It consist of single piece of information. It cannot be divided into other meaningful
pieces of data. e.g Name of Person, Name of Book
Composite Data
It can be divided into subfields that have meaning.
e.g. Address, Telephone number
Percentage
Data Object represents an object having a data.
For Example:
If student is one object then it will consist of different data like roll no,
name, percentage , address etc.
5
DATA STRUCTURE -I UNIT-I
Data, Data Objects and Data Types
7
DATA STRUCTURE -I UNIT-I
Abstract data types
An abstract data type is a type with associated operations, but whose representation is
hidden.
8
Abstract Data Types (ADT)
An ADT is composed of
A collection of data
A set of operations on that data
Implementation of an ADT
Includes choosing a particular data structure
9
Data Structure
DATA STRUCTURE:- Structural representation of data items in primary
memory to do storage & retrieval operations efficiently.
Data may be organized in many different ways,
The logical or mathematical model of a particular organization of data is
called data structure.
Data objects + Relationship among the instances
Data Structure Study concerns with data object representation and
functions implementation
Data Structure
Types of Data Structures
Linear data structure:
The data structure where data items are organized sequentially or linearly where data
elements attached one after another is called linear data structure. It has unique
predecessor and Successor.
13
Types of Data Structures
Ephermal :
• Can not retain their previous state
14
Types of Data Structures
15
Introduction to Algorithms
Algorithm
◦ Solution to a problem that is independent of any programming language.
◦ Sequence of steps required to solve the problem
◦ Algorithm is a finite set of instructions that if followed, accomplishes a particular task
◦ All algorithms must satisfy the following criteria:
❑Input: Zero or more Quantities are externally supplied
❑Output: At least one quantity is produced
❑Definiteness: Each instruction is clear and unambiguous
❑Finiteness: if we trace out the instructions of an algorithm then for all cases the algorithm
terminates after a finite number of steps.
❑Effectiveness: Every instruction must be very basic so that it can be carried out in principle by a
person using pencil and paper.
17
Introduction to Algorithms
18
Introduction to Algorithms
Defining actions in Find Largest algorithm
19
Introduction to Algorithms
Find Largest refined
20
Introduction to Algorithms
21
Introduction to Algorithms
Three constructs
22
Algorithm Design Tools
➢Pseudo Code
❑is an artificial and informal language that helps programmers develop algorithms.
❑Uses English-like phrases with some Visual Basic terms to outline the program
➢Flowchart
Algorithm
Step 1: Initialize X as 0,
Step 2: Increment X by 1,
Step 3: Print X,
24
Pseudo Code
Algorithm SORT(A, n)
Pseudocode is an informal high-level description {
of the operating principle of a computer program or for (i =1;i<n; i++)
other algorithm. {
j=i;
for (k = j+1;k<n; k++)
It uses the structural conventions of a normal {
programming language, but is intended for human if A[k] < A[j]
reading rather than machine reading. j=k;
}
t = A[i];
A[i]= A[j];
A[j]=t
}
}
Semicolons used
❑Words such as set, reset, increment, compute, calculate, add, sum, multiply, ... print, display,
input, output, edit, test , etc. with careful indentation tend to foster desirable pseudocode.
❑Also, using words such as Set and Initialize, when assigning values to variables is also
desirable.
❑But do cite variables that are initialized as part of their declarations. E.g. "initialize
count to zero" is a good entry.
Pseudocode Disadvantages
It’s not visual
There is no accepted standard, so it varies widely from company to
company
Pseudocode Advantages
Can be done easily on a word processor
Easily modified
Implements structured concepts well
Hard to modify
Need special software
Flowchart Advantages
Standardized: all pretty much agree on the symbols and their meaning
Visual
1.Repeat Read
Process Read
Process Calc Calc
Process Print
Until
Print
NoMoreEmployee False
Until
noMoreEm
ployee
2.End
True
End
1. Read Hours,
2. Read
PayRate Read
Hours,
PayRate
3.Exit
Exit
1. GrossPay=
HoursWorked
*PayRate
GrossPay=
HoursWorked*PayRate
2.Exit
Exit
1. Print Pay
Exit
Read
Hours,Pay
Rate
Read
GrossPay=
Calc Hours*PayRate
Print
Exit
False
Until
Print
noMoreEm
ployee
True Print
GrossP
End ay
Exit
Time Complexity
Space Complexity
41
complexity
It is the time taken and space required for an algorithm for its
completion
It decides the quality of algorithm
◦ Two types
◦ Space complexity
◦ Time complexity
Space complexity
Amount of space required by an algorithm
It is the sum of fixed part and variable part
Fixed part : it depends on the characteristics of i/p and o/p
Variable part : instance characteristics
Time complexity
Sum of compile time and execution time
Time complexity is amount of time required by algorithm for
completion of problem .
There are three cases
◦ Best case
◦ Worst case
◦ Average case
Best case
If an algorithm takes an minimum amount of time to run to completion
for specific set of input
Eg. While searching a particular element by using sequential search we
get desired element at first place itself .
Worst case
If an algorithm takes an Maximum amount of time to run to completion
for specific set of input
Eg. While searching a particular element by using sequential search we
get desired element at end.
Average case : the average time taken by an algorithm to run to
completion is called average case.
◦ This classification gives complexity information about behavior of algorithm
at particular instance .
Running Time
•The running time depends on the input: an already
sorted sequence is easier to sort.
N 0, 1, 2, ...
Asymptotic
Notation
There are 5 types
Big-oh Notation (O- notation)
Omega Notation (Ω - notation)
Theta Notation (Θ - notation)
Little –oh Notation (o- notation)
Little –omega Notation (ω- notation)
O-notation
For function g(n), we define O(g(n)),
big-O of n, as the set:
O(g(n)) = {f(n) :
∃ positive constants c and n0,
such that ∀n ≥ n , 0
Ω(g(n)) = {f(n) :
∃ positive constants c and n0,
such that ∀n ≥ n , 0
}
Intuitively: Set of all functions that
have the same rate of growth as g(n).
– Best Case
The amount of time a program might be expected to take on best possible input
data
– Worst Case
The amount of time a program might be expected to take on typical(or average)
input data
– Average case
The amount of time a program would take on worst possible input configuration.
Example: Sorting Algorithms DATA STRUCTURE -I UNIT-I 54
FAQ
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
for(k=1;k<=p;k++)
Sum=sum+i
Pseudo code and flowcharts are the tools used to represent the solution of a
complexity.
69
DATA STRUCTURE -I UNIT-I
References
70
DATA STRUCTURE -I UNIT-I