Basic
Basic
Basic Concepts
1
Definition
◼ Data structure is representation of the logical
relationship existing between individual
elements of data.
◼ In other words, a data structure is a way of
organizing all data items that considers not
only the elements stored but also their
relationship to each other.
Introduction
◼ Data structure affects the design of both
structural & functional aspects of a program.
Program=algorithm + Data Structure
◼ You know that a algorithm is a step by step
Data structure
Primitive DS Non-Primitive DS
Non-Primitive DS
◼ A Set of Instructions
◼ Data Structures + Algorithms
◼ Data Structure = A Container stores Data
◼ Algoirthm = Logic + Control
12
Efficiency
◼ Computer Scientists don’t just write programs.
◼ They also analyze them.
◼ How efficient is a program?
◼ How much time does it take program to complete?
◼ How much memory does a program use?
◼ How do these change as the amount
of data changes?
◼ What is the difference between the average case
and worst-case efficiency if any?
13
Technique
◼ How many computations will this program
(function, algorithm) perform to get the
answer?
◼ Many simplifications
◼ view algorithms as C programs
◼ determine by analysis the total number
executable statements (computations) in
program or as a function of the amount of
data
Counting Statements
int x; // one statement
x = 12; // one statement
int y = z * x + 3 % 5 * x / i; // 1
x++; // one statement
int p = x < y && y % 2 == 0 ||
z >= y * x; // 1
Int data[100]; // 100
data[50] = x * x + y * y; // 1
15
Example
int total(int values[],int n) {
int result = 0,i;
for (i= 0; i < n; i++)
result += values[i];
return result;
}
16
Big O notation – Calculating Algorithm Efficiency
17
Big O notation
A simplified analysis of algorithm efficiency.
It considers:
• Complexity in terms of input size, N
• Machine independence (Doesn’t consider the machines
hardware)
• Basic computer steps
• Time & space(working storage)
• Originally used by German theorist Paul Bachman
• O means order of (German: "Ordnung von").
• n is the input size in units of bits needed to represent
the input
18
Big O
◼ The most common method and notation for
discussing the execution time of algorithms is Big
O, also spoken Order
◼ Big O is the asymptotic execution time
of the algorithm
◼ In other words, how does the running time of
23
Asymptotic Notations
Following are the commonly used asymptotic
notations to calculate the running time
complexity of an algorithm.
•Ο Notation
•Ω Notation
•θ Notation
24
Big Oh Notation, Ο
• The notation Ο(n) is the formal way to express the upper
bound of an algorithm's running time.
• It measures the worst-case time complexity or the
longest amount of time an algorithm can possibly take to
complete.
Omega Notation, Ω
• The notation Ω(n) is the formal way to express the lower
bound of an algorithm's running time.
• It measures the best-case time complexity or the best
amount of time an algorithm can possibly take to
complete.
Theta Notation, θ
• The notation θ(n) is the formal way to express both the
lower bound and the upper bound of an algorithm's
running time. 25
Data Structure
Aggregation of atomic and composite data into a set with defined
relationships. Structure refers to a set of rules that hold the data
together.
• A combination of elements in which each is either a data type or
another data structure.
• A set of associations of relationship involving combined elements.
26
• Atomic and Composite Data
• Data Type
• Data Structure
• Abstract Data Type
27
28
The Abstract Data Type
The abstract datatype is special kind of datatype, whose
behavior is defined by a set of values and set of operations.
29
Stack −
• isFull(), This is used to check whether stack is full
or not
• isEmpty(), This is used to check whether stack is
empty or not
• push(x), This is used to push x into the stack
• pop(), This is used to delete one element from top
of the stack
• peek(), This is used to get the topmost element of
the stack
• size(), this function is used to get number of
elements present into the stack
30
Queue −
• isFull(), This is used to check whether queue is
full or not
• isEmpry(), This is used to check whether queue is
empty or not
• insert(x), This is used to add x into the queue at
the rear end
• delete(), This is used to delete one element from
the front end of the queue
• size(), this function is used to get number of
elements present into the queue
31
List −
• size(), this function is used to get number of
elements present into the list
• insert(x), this function is used to insert one element
into the list
• remove(x), this function is used to remove given
element from the list
• get(i), this function is used to get element at
position i
• replace(x, y), this function is used to replace x with
y value
32
ADT Implementations
33
Lists
A list is an ordered set of data. It is often used to store
objects that are to be processed sequentially.
34
Arrays
An array is an indexed set of variables, such as
dancer[1], dancer[2], dancer[3],… It is like a set of
boxes that hold things.
An array is a set of
variables that each
store an item.
35
Arrays and Lists
In a list, the missing spot is filled in when
something is deleted.
36
Arrays and Lists
In an array, an empty variable is left behind
when something is deleted.
37
38
39
40
41