A979968895 - 21482 - 28 - 2020 - Ds 1-Basic Data Structure
A979968895 - 21482 - 28 - 2020 - Ds 1-Basic Data Structure
• Data structures are the basis of the programming tools. The choice of data
structures should provide the following:
1. The data structure should satisfactorily represent the relationship between the
data elements.
2. The data structure should be easy so that the programmer can easily process the
data.
Classification of Data Structures
• Linear: The values are arranged in a linear fashion. E.g. Arrays, Linked Lists, Stacks,
Queues etc.
• Non-Linear: The values are not arranged in an order. E.g. Tree, Graph, Table etc.
• Homogeneous: Here all the values stored are of same type e.g. arrays
• Non- Homogeneous: Here all the values stored are of different type e.g. structures and
classes.
• Dynamic:A dynamic data structure is one that can grow or shrink as needed to contain
the data you want stored. That is, you can allocate new storage when it's needed and
discard that storage when you're done with it. E.g. pointers, or references
• Static:They're essentially fixed-size and often use much space E.g. Array
Basic Notations
Algorithmic Notations:
Finite step by step list of well-defined instructions for solving a particular problem.
Formal presentation of the Algorithm consists of two parts:
1. A paragraph that tells the purpose of an algorithm. Identifies the variables which occur in the algorithm
and lists the input data.
2. The list of the steps that is to be executed.
Some conventions used in Algorithms:
Steps, Control and Exit ( All statements are numbered, Go to and Exit)
Input and output (Read: variable names, Write: Messages and/or variable names)
Procedure
Basic Notations
Control Structures:
-----
[Endif]
• If (condition) , then:
-----
Else :
-------
[ Endif]
• Multiple If (condition), then : -----
Elseif : -----
Else :
------
[Endif]
Primitive Non-Primitive
Data Structures Data Structures
• Integer Linear Non-Linear
• Real Data Structures Data Structures
• Character • Array • Tree
• Boolean • Stack • Graph
• Queue
• Linked List
Data Structure Operations
Data Structures are processed by using certain operations.
1.Traversing: Accessing each record exactly once so that certain
items in the record may be processed.
statement.
Also known as algorithm complexity
Time Complexity
Worst-case
An upper bound on the running time for any
input of given size
Average-case
Assume all inputs of a given size are equally
likely
Best-case
The lower bound on the running time
Time Complexity – Example
Sequential search in a list of size n
Worst-case:
n comparisons
Best-case:
1 comparison
Average-case:
n/2 comparisons
time space tradeoff
A time space tradeoff is a situation where the
memory use can be reduced at the cost of slower
program execution (and, conversely, the
computation time can be reduced at the cost of
increased memory use).
}
Example 11
while(n>=1)
{
n=n-20;
n=n+5;
n=n-30;
}
Example 12
while(n>=1)
{
n=n/2;
}
Example 13
while(n>=1)
{
n=n/2;
n=n/3;
}
Example 14
while(n>=1)
{
n=n-2;
n=n/2;
}
Example 15
for(int i = 1; i < n; i *= 2) {
cout << i << endl;
}
There are n iterations, however, instead of
simply incrementing, 'i' is increased by
2*itself each run. Thus the loop is log(n).
Example 16
for(int i = 0; i < n; i++) { //linear
for(int j = 1; j < n; j *= 2){ // log (n)
//do constant time stuff
}
}
Ans: n*log(n)
Example 17
while(n>2)
{
n=√n;
}
Example 18
while(n>2)
{
n=n2;
n=√n;
n=n-2;
}
Example 19
x=y+z;
}
Example 20
Main()
{
While(n>2)
{
n=√n;
n=n/2;
n=n-2;
}
}
Example 21
main()
{
While(n>1)
{
n=n/2;
a=a+b;
}
}
Example 22
main()
{
While(n>=1)
{
n=n/2;
}
}
Example 23
main()
{
While(n3>1)
{
n=n-1;
a=b+c;
X=x+y;
}
}
Example 24
main()
{
for(i=1;i<=n;i=3*i)
{j=1;
While(j<=n)
{
j=j*3;
}
}
}
Example 25
main()
{
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
for(k=1;i<=143;k++)
{
x=y+z;
}
}
}
}
Comp 122
Thank You