Unit-1 - Introduction To DS
Unit-1 - Introduction To DS
GTU # 3130702
Unit-1
Introduction to
Data Structure
Prepared By:
Tushar Jakhaniya
Outline
Looping
Data Management concepts
Data types
• Primitive
• Non-primitive
Types of Data Structures
• Linear Data Structures
• Non Linear Data Structures
Performance Analysis and Measurement
Time analysis of algorithms
Space analysis of algorithms
What is Data?
Data is the basic fact or entity that is utilized in calculation or manipulation.
There are two different types of data Numeric data and Alphanumeric data.
When a programmer collects such type of data for processing, he would require to store
them in computer’s main memory.
The process of storing data items in computer’s main memory is called representation.
Data to be processed must be organized in a particular fashion, these organization leads to
structuring of data, and hence the mission to study the Data Structures.
What is Data Structure?
Data Structure is a 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.
We can also define data structure as a mathematical or logical model of a particular
organization of data items.
Data Structure mainly specifies the following four things
Organization of Data
Accessing Methods
Degree of Associativity
Processing alternatives for information
What is Data Structure? Cont..
The representation of a particular data structure in the memory of a computer is called
Storage Structure.
The storage structure representation in auxiliary memory is called as File Structure.
Floating Pointers
Point Linear Non-linear
List List
Array
List
Linear / Non-Linear data structure
Linear data structures
A data structure is said to be Linear, if its elements are connected in linear fashion by means of logically or
in sequence memory locations.
Examples of Linear Data Structure are Stack, Queue, Array, Linked-list.
Nonlinear data structures
Nonlinear data structures are those data structure in which data items are not arranged in a sequence.
Examples of Non-linear Data Structure are Tree and Graph.
Problem-solving using computer requires memory to hold temporary data or final result
while the program is in execution. The amount of memory required by the algorithm to solve
given problem is called space complexity of the algorithm.
It is the amount of memory needed for the completion of an algorithm.
To estimate the memory requirement we need to focus on two parts:
(1) A fixed part: It is independent of the input size. It includes memory for instructions
(code), constants, variables, etc.
(2) A variable part: It is dependent on the input size. It includes memory for recursion stack,
referenced variables, etc.
Time and space analysis of algorithms
Sometimes, there are more than one way to solve a problem.
We need to learn how to compare the performance of different algorithms and choose the best
one to solve a particular problem.
While analyzing an algorithm, we mostly consider time complexity and space complexity.
Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run
as a function of the length of the input.
Space complexity of an algorithm quantifies the amount of space or memory taken by an
algorithm to run as a function of the length of the input.
Time & space complexity depends on lots of things like hardware, operating system,
processors, etc.
However, we don't consider any of these factors while analyzing the algorithm. We will only
consider the execution time of an algorithm.
Calculate Time Complexity of Algorithm
Time Complexity is most commonly estimated by counting the number of elementary
functions performed by the algorithm .
It is called as frequency count.
For (i=0;i<=n;i++)
For (j=0;J<=n;j++)
Statement Frequency Count
X=x+1;
i=0 1
i<=n n+1
i++ n
J=0 n
J<=n n(n+1)
J++ N*N
X++ N*N
total 3N2+4N+2
Calculating Time Complexity
Calculate Time Complexity of Sum of elements of List (One dimensional Array)
A is array, n is no of elements in
SumOfList(A,n) array
{ Line No of Times
Line 1 total = 0 1 1
Line 2 for i = 0 to n-1 2 n+1
Line 3 total = total + A[i] 3 n
Line 4 return total 4 1
}
TSumOfList = 1 + 2 (n+1) + 2n +
1 = 4n + 4 We can neglate constant 4
=n
Based on the above three notations of Time Complexity there are three cases to analyze an
algorithm:
1. Worst Case Analysis
the worst-case analysis, we calculate the upper bound on the running time of an algorithm.
We must know the case that causes a maximum number of operations to be executed.
2. Best Case Analysis
In the best-case analysis, we calculate the lower bound on the running time of an algorithm.
We must know the case that causes a minimum number of operations to be executed.
3. Average Case Analysis
n average case analysis, we take all possible inputs and calculate the computing time for all
of the inputs.
Big-O notation (O-notation):
Big-O notation represents the upper bound of the running time of an algorithm. Therefore, it
gives the worst-case complexity of an algorithm.
It is the most widely used notation for Asymptotic analysis. It specifies the upper bound of a
function. The maximum time required by an algorithm or the worst-case time complexity.
If f(n) describes the running time of an algorithm, f(n) is O(g(n)) if there exist a positive constant C and n0 such
that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0
Omega Notation (Ω-Notation):
Omega notation represents the lower bound of the running time of an algorithm. Thus, it
provides the best case complexity of an algorithm.
The execution time serves as a lower bound on the algorithm’s time complexity.
It is defined as the condition that allows an algorithm to complete statement execution in the
shortest amount of time.
Let g and f be the function from the set of natural numbers to itself. The function f is said to be Ω(g), if there is
a constant c > 0 and a natural number n0 such that c*g(n) ≤ f(n) for all n ≥ n0
Theta Notation (Θ-Notation):
Theta notation encloses the function from above and below. Since it represents the upper and
the lower bound of the running time of an algorithm, it is used for analyzing the average-
case complexity of an algorithm.
Theta (Average Case) You add the running times for each possible input combination and
take the average in the average case.
Let g and f be the function from the set of natural numbers to itself. The function f is said to be Θ(g), if there are
constants c1, c2 > 0 and a natural number n0 such that c1* g(n) ≤ f(n) ≤ c2 * g(n) for all n ≥ n0
Array
Array is group of Elements having same datatype.
Array is an ordered set which consist of fixed number of elements of
same data type.
Array is a linear data structure.
Syntax:
Datatype arrayname [arrraysize];
Conti.
All the element of array shares the common name and they are distinguished with the help of
element number.
Array elements are always stored sequentially or in linear fashion.
In array, Insertion and deletion operation is slower and time consuming but searching and
sorting operation is faster.
When array is declared and not initialized, it contains garbage values. If array is declared as
static, all elements are initialized to zero.
A[1][2] Row
1
Row A[1][1] A[1][2] A[1][3]
1 A[1][3]
A[2][1]
A[2][1] A[2][2] A[2][3]
Row
2 A[2][2] Row
2
A[2][3]
Conti.
Array :
LOC(A[i]) = Base Address + W * (i)
W= word Size
Example : base address =2000, W=2
LOC(A[i]) = Base Address + W * (i)
Loc(A[2]) = 2000 + 2 * (2)
= 2004
Row Major Array
Address of A [i , j ] cann be given as-
1. Linear Search :
this method is also known as Sequential Search. It find element from sorted as well as
unsorted list.
Index Element
1 11
2 33
3 55
4 22
5 44
Binary Search
Binary Search only work with sorted list.
Binary search Algorithm:
Algorithm:
1. low <- 0
high <- N-1
2. repeat up to step 4 while low<=high
3.mid<- (low+high) /2
4.If X<A[mid] then
high<- mid-1
Else if X> A[mid] then
Low <- mid+1
Else
write "search successful"
return 1
5. Write "search is not sucessful"
Return 0
Thank You…