UNIT-1 Introduction to Data Structure
UNIT-1 Introduction to Data Structure
{ union union_example
{
member definition; int integer;
float decimal;
member definition; ... char name[20];
member definition; };
};
Structure v/s Union
struct struct_example
union union_example
{
{
int integer; 2 bytes int integer; 2 bytes
Total: Max (integer,
float decimal; 4 bytes 26 bytes
float decimal; 4 bytes decimal, name[20])
char name[20]; 20 bytes char name[20]; 20 bytes = 20 bytes
};
};
In above example, structure will occupy 26 bytes where as union will occupy 20 bytes.
DIFFERENCE BETWEEN STRUCTURE AND
UNION
Pointer
• Pointer stores the address of another variable. It refers to a
memory location.
Features of Pointer
• Pointers save memory space
• Execution time of using pointer is faster
• Pointers are used with data structure
• Pointers are used in file handling
Pointer Declaration
• The general form is
data_type *variableName;
• Where * is Indirection operator / Dereference operator
• Examples
int *iptr; //iptr is a pointer to an integer variable
float *fptr; //fptr is a pointer to a float variable
char *cptr; //cptr is a pointer to a character
const int *ptric; //ptric is a pointer to an integer constant
Assigning Values to pointer using ‘&’
operator:
• Declare a pointer
• Assign the address of a variable to a 1000
pointer 1001
10 a
• Example 1002
• Queue: The data structure which permits the insertion at one end and
Deletion at another end, known as Queue. Queue is also called as First
in First out (FIFO) data structure.
Linear Vs Non Linear Data Structures
Linear Data Structure Non-Linear Data Structure
• Example: recipe to make tea, algorithm to find maximum common divisor of two
integers, algorithm to find factorial of a number.
• An algorithm is correct, if, for each input instance, it gets the correct output and
gets terminated.
Contd…
• An algorithm is the best way to represent the solution of a particular problem in a
very simple and efficient way.
• Pseudocode Approach:
1. Count <- 0, absent <- 0, total <- 60
2. REPEAT till all students counted
Count <- Count + 1
3. absent <- total - Count
4. Print "Number absent is:" , absent
Performance analysis of an algorithm
• Which is the best algorithm to solve any problem? How do we judge? How do we
identify?
• The analysis is a process of estimating the efficiency of an algorithm as well as
calculating/predicting the resources that the algorithm requires. .
• There are two fundamental parameters based on which we can analysis the
algorithm:
• Space Complexity: The space complexity can be understood as the amount of
space required by an algorithm to run to completion.
• Time Complexity: Time complexity is a function of input size n that refers to the
amount of time needed by an algorithm to run to completion.
Why analysis is required?
• To know in advance about resources (time & memory) requirement.
• To compare 2 or more algorithms designed to solve a particular
problem and select the algorithm with minimum resource (time &
memory) requirement
• To optimize resource requirement (time-space) of an algorithm
Analysis of Algorithm
Best case Average case Worst case
Resource usage is Resource usage is Resource usage is
minimum average maximum
Algorithm’s behavior Algorithm’s behavior Algorithm’s behavior
under optimal under random under the worst
condition condition condition
Minimum number of Average number of Maximum number of
steps or operations steps or operations steps or operations
Lower bound on Average bound on Upper bound on
running time running time running time
Generally do not Average and worst-case performances are
occur in real the most used in algorithm analysis.
Number Sorting
• Suppose you are sorting numbers in Ascending order.
• The initial arrangement of given numbers can be in any
of the following three orders.
1. Numbers are already in required order, i.e., Ascending order
No change is required – Best Case
2. Numbers are randomly arranged initially.
Some numbers will change their position – Average Case
3. Numbers are initially arranged in Descending or Decreasing
order.
All numbers will change their position – Worst Case
5 9 12 23 32 41
9 5 12 32 23 41 5 9 12 23 32 41
41 32 23 12 9 5
Best, Average, & Worst Case
Best Case Average Case Worst Case
Problem
• The idea behind the big-O notation is to establish an upper bound (upper limit)
for the growth of a function f(n) for large n.
• We accept the constant C in the requirement
f(n) ≤ C*g(n) whenever n ≥ n0,
because C does not grow with x
• We are only interested in large n, so it is OK if f(n) > C*g(n) for n < n0.
Contd…
• Graphically, it can be represented as follows:
Big – O notation Examples
• Prove that f(n) = 3n + 5 is O(n).
f(n) = 3n + 5
≤ 3n + n , where n ≥ 5
≤ 4n
≤ C g(n) , where C=4 & n0=5
• Hence, f(n) = 3n + 5 is O(n) is proved.
• Similarly, Prove for the followings:
• f(n) = 27n2 + 16n is O(n2).
• f(n) = 2n3 + n+ + 2n is O(n3).
• f(n) = 4n3 + 2n + 3 is O(n3).
• f(n) = 2n + 6n2 + 3n is O(2n).
Big – O notation incorrect bound
• Prove that f(n) = 7n + 5 ≠ O(1).
• Proof by contradiction:
• Assume that 7n + 5 = O(1). So, we must have
• 7n + 5 ≤ C.1 for n ≥ n0.
• above statement is not true for large values of n
• Hence, our assumption is false.
• Hence, f(n) = 3n + 5 is O(n) is proved.
• Similarly, Prove for the followings:
• f(n) = 10n2 + 7 ≠ O(n).
• f(n) = 27n2 + 16n + 25 ≠ O(n).
• f(n) = 3n3 + 4n ≠ O(n2).
Big – O notation Loose bounds
• Question: If f(x) is O(x2), is it also O(x3)?
• Yes. x3 grows faster than x2, so x3 grows also faster than f(x).
• Therefore, we always have to find the smallest (closest) standard function g(x) for
which f(x) is O(g(x)).
• Yes.
• Therefore, we always have to find the Largest simple function g(x) for which f(x)
is (g(x)).
similarly, f(n) = 3n + 5
≤ 2n + n , where n ≥ 5
≤ 4n
≤ C2 g(n) , where C2=4
• Big-Ο is used as a tight upper-bound for the growth of an algorithm, where as little o
means loose upper-bound.
Contd…
• Example : consider f(n)=2n+3, g(n)=n2
• = = == = =0
• Solve: If f(n) = n2 and g(n) = n3 then check whether f(n) = o(g(n)) or not.
Little – ω notation
• Definition : Let f(n) and g(n) be functions that map positive integers to positive real
numbers. If for any real constant c > 0, there exists an integer constant n 0 ≥ 1 such that
f(n) > c * g(n) ≥ 0 for every integer n ≥ n0, then we say that
f(n) is ω(g(n)) OR f(n) ∈ ω(g(n))
• Mathematical definition: Let f(n) and g(n) are two non-negative functions. If
[f(n) / g(n)]= then we say that f(n)= ω (g(n)).
• Big- Ω is used as a tight lower-bound for the growth of an algorithm, where as little ω
means loose lower-bound.
Contd…
• Example : consider f(n)= 4n2+6, g(n)= n
• = = = ==
• Solve: If f(n) = 2n-3 and g(n) =1 then check whether f(n) = ω(g(n)) or not.
Common time complexities
BETTER
O(1) Constant time
O(log n) Logarithmic time
O(n) Linear time
O(n log n) Logarithmic linear time
O(n2) Quadratic time
O(n3) Cubic time
O(2n) Exponential time
WORSE
O(n!) Factorial time
O(nn)