DSA - Unit 1
DSA - Unit 1
Time Complexity is
O(Log n)
• Space Complexity
o We just need to store three values: `lower_bound`,
`upper_bound`, and `middle_position`.
Space Complexity is
O(1)
Review Questions
• The worst case complexity is ______ when compared with the
average case complexity of a binary search algorithm.
(a) Equal (b) Greater (c) Less (d) None of these
• The complexity of binary search algorithm is
(a) O(n) (b) O(n2) (c) O(n log n) (d) O(log n)
• Which of the following cases occurs when searching an array
using linear search the value to be searched is equal to the first
element of the array?
(a) Worst case (b) Average case (c) Best case (d) Amortized case
• Performance of the linear search algorithm can be improved by
using a ______.
• The complexity of linear search algorithm is ______.
ALGORITHM
SORTING
SORTING
• Sorting is a technique to rearrange the elements of a list in
ascending or descending order, which can be numerical,
lexicographical, or any user-defined order.
• Sorting is a process through which the data is arranged in
ascending or descending order.
Sorting can be classified in two types;
1. Internal Sorts
2. External Sorts
SORTING - INTERNAL SORTS
• Internal Sorts:- This method uses only the primary memory during
sorting process. All data items are held in main memory and no
secondary memory is required for this sorting process.
• If all the data that is to be sorted can be accommodated at a time
in memory is called internal sorting. There is a limitation for
internal sorts; they can only process relatively small lists due to
memory constraints. There are 3 types of internal sorts based on
the type of process used while sorting.
(i) SELECTION :- Ex:- Selection sort algorithm, Heap Sort
algorithm
(ii) INSERTION :- Ex:- Insertion sort algorithm, Shell Sort
algorithm
(iii) EXCHANGE :- Ex:- Bubble Sort Algorithm, Quick sort
SORTING – EXTERNAL SORTS
• External Sorts:- Sorting large amount of data requires external or
secondary memory. This process uses external memory such as
HDD, to store the data which is not fit into the main memory. So,
primary memory holds the currently being sorted data only.
• All external sorts are based on process of merging. Different parts
of data are sorted separately and merged together. Ex:- Merge
Sort
INSERTION SORT
• Insertion Sort Algorithm sorts array by shifting elements one by one and inserting
the right element at the right position
• It works similar to the way you sort playing cards in your hands
INSERTION SORT
• Insertion sorts works by
taking elements from the list Pseudo code
one by one and inserting them
in their current position into a INSERTION-SORT(A)
new sorted list.
for i = 1 to n
• Insertion sort consists of N - 1 key ← A [i]
passes, where N is the j←i–1
number of elements to be while j > = 0 and A[j] > key
sorted. A[j+1] ← A[j]
• The ith pass of insertion sort j←j–1
End while
will insert the ith element A[i]
A[j+1] ← key
into its rightful place among End for
A[1], A[2] to A[i - 1]. After
doing this insertion the
records occupying A[1]....A[i]
are in sorted order.
Working of insertion sort – Ascending order
•We start by considering the second element of the given array, i.e.
element at index 1, as the key.
•We compare the key element with the element(s) before it, i.e., element
at index 0:
– If the key element i.e index 1 is less than the first element, we insert
the key element before the first element.(swap)
– If the key element is greater than the first element, then we insert it
remains at its position
– Then, we make the third element of the array as key and will
compare it with elements to it's left and insert it at the right
position.
•And we go on repeating this, until the array is sorted
.
Example
Let us now understand working
with the following example:
Consider the following array:
25, 17, 31, 13, 2 where N=5
Second Iteration
•The same process goes on for
the remaining iterations.
•After each iteration, the largest
element among the unsorted
elements is placed at the end.
The array is sorted if all elements are kept in the right order.
Compare the adjacent elements Now all the elements are sorted
ADVANTAGES AND DISADVANTAGES
Advantage:
•It is the simplest sorting approach.
•The primary advantage of the bubble sort is that it is popular and easy to
implement.
•In the bubble sort, elements are swapped in place without using additional
temporary storage. Stable sort: does not change the relative order of elements with
equal keys.
•The space requirement is at a minimum
Disadvantage:
•Bubble sort is comparatively slower algorithm.
•The main disadvantage of the bubble sort is the fact that it does not deal well with
a list containing a huge number of items.
•The bubble sort requires n-squared processing steps for every n number of
elements to be sorted.
COMPLEXITY – TIME ,
SPACE TRADE OFF
SPACE COMPLEXITY
• Space complexity is the total amount of memory space used
by an algorithm/program including the space of input
values for execution. So to find space-complexity, it is enough
to calculate the space occupied by the variables used in an
algorithm/program.
• Space complexity S(P) of any algorithm P is,
• S(P) = C + SP(I)
• Where C is the fixed part and S(I) is the variable part of the
algorithm which depends on instance characteristic I.
Example: Here we have three variables A, B & C and one
Algorithm: SUM(A, B) constant. Hence S(P) = 1+3.
Step 1 - START Space requirement depends on data types of
Step 2 - C ← A + B + 10 given variables & constants. The number will be
multiplied accordingly.
Step 3 – Stop
How to calculate Space Complexity of an Algorithm?
• In the code given above, the array stores a maximum of n integer elements.
Hence, the space occupied by the array is 4 * n. Also we have integer
variables such as n, i and sum.
• Assuming 4 bytes for each variable, the total space occupied by the program
is 4n + 12 bytes.
• Since the highest order of n in the equation 4n + 12 is n, so the space
complexity is O(n) or linear.
EXAMPLE 3
#include<stdio.h>
int main()
{
int a = 5, b = 5, c;
c = a + b;
printf("%d", c);
}
Space Complexity: size(a)+size(b)+size(c)
=>let sizeof(int)=2 bytes=>2+2+2=6 bytes
=>O(1) or constant
Example 4
#include <stdio.h> Space Complexity:
int main()
• The array consists of n
{
integer elements.
int n, i, sum = 0;
scanf("%d", &n); • So, the space occupied by the
int arr[n]; array is 4 * n. Also we have
for(i = 0; i < n; i++) integer variables such as n, i
{ and sum. Assuming 4 bytes
scanf("%d", &arr[i]); sum = sum + arr[i]; for each variable, the total
} space occupied by the
printf("%d", sum); program is 4n + 12 bytes.
} • Since the highest order of n
in the equation 4n + 12 is n,
so the space complexity is
O(n) or linear.
Time Complexity
• Time Complexity of an algorithm represents the amount of
time required by the algorithm to run to completion.
• Time requirements can be defined as a numerical function
T(n), where T(n) can be measured as the number of steps,
provided each step consumes constant time.
• Eg. Addition of two n-bit integers takes n steps.
• Total computational time is T(n) = c*n,
• where c is the time taken for addition of two bits.
• Here, we observe that T(n) grows linearly as input size
increases.
Time Complexity (Cont..)
Two methods to find the time complexity for an algorithm
1. Count variable method
2. Table method
9/11/21 71
Count Variable Method
72
Table Method
• The table contains s/e and frequency.
9/11/21 73
Example 1
9/11/21 74
Example 2
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}
• In above calculation
Cost is the amount of computer time required for a single operation in each line.
• Repetition is the amount of computer time required by each operation for all its repetitions.
Cost Time
s
i = 1; c1 1
sum = 0; c2 1
whil e (i <= n) { c3 n+1
i = i + 1; c4 n
sum = su m + i; c5 n
}
3n+3
Total Cost = c1 + c2 + (n+1)*c3 + n*c4 + n*c5
The time required for this algorithm is proportional to n
Example 5
83
2.
3.
9/11/21 84
MATHEMATICAL
NOTATIONS
1. Floor and Ceiling Functions
2. Remainder Function (Modular
Arithmetic)
3. Integer and Absolute Value
Functions
4. Summation Symbol (Sums)
5. Factorial Function
6. Permutations
7. Exponents and Logarithms
ASYMPTOTIC
NOTATIONS
BIG O, OMEGA
Asymptotic Analysis
• The time required by an algorithm falls under three types −
• Best Case − Minimum time required for program execution.
• Average Case − Average time required for program
execution.
• Worst Case − Maximum time required for program
execution.
94
Asymptotic Analysis (Cont..)
• Asymptotic analysis of an algorithm refers to defining the
mathematical boundation/framing of its run-time performance.
• Derive the best case, average case, and worst case scenario of
an algorithm.
• Asymptotic analysis is input bound.
• Specify the behaviour of the algorithm when the input size
increases
• Theory of approximation.
• Asymptote of a curve is a line that closely approximates a curve
but does not touch the curve at any point of time.
95
Asymptotic notations
• Asymptotic notations are mathematical tools to represent the
time complexity of algorithms for asymptotic analysis.
• Asymptotic order is concerned with how the running time of an
algorithm increases with the size of the input, if input increases
from small value to large values
9/11/21 96
Big-Oh Notation (O)
• Big-oh notation is used to define the worst-case running time of an
algorithm and concerned with large values of n.
9/11/21 97
Big-Oh Notation (O)
9/11/21 98
Big-Oh Notation (O)
1 < log n < √n < n < n logn < n2 < n3 < ……………< 2n < 3n < …. <nn
2n + 3 ≤ 5n n≥1
here c = 5 and g(n) = n
t(n) = O(n)
2n + 3 ≤ 5n2 n≥1
here c = 5 and g(n) = n2
t(n) = O(n2 )
99
Big-Omega notation (Ω)
• This notation is used to describe the best case running time of
algorithms and concerned with large values of n.
9/11/21 100
Big-Omega notation (Ω)
9/11/21 101
Big-Omega notation (Ω)
1 < log n < √n < n < n logn < n2 < n3 < ……………< 2n < 3n < …. <nn
2n + 3 ≥ 1n n≥1
here c = 1 and g(n) = n
t(n) = Ω(n)
2n + 3 ≥ 1log n n ≥ 1
here c = 1 and g(n) = log n
t(n) = Ω(log n)
9/11/21 102
Asymptotic Analysis of Insertion
sort
• Time Complexity:
• Best Case: the best case occurs if the array is already sorted, tj=1 for
j=2,3…n.
9/11/21 103
Asymptotic Analysis of Insertion
sort
• Worst case : If the array is in reverse sorted order
9/11/21 104
Properties of O, Ω and θ
General property:
If t(n) is O(g(n)) then a * t(n) is O(g(n)). Similar for Ω and θ
Transitive Property :
If f (n) ϵ O(g(n)) and g(n) ϵ O(h(n)), then f (n) ϵ O(h(n)); that
is O is transitive. Also Ω, θ, o and ω are transitive.
Reflexive Property
If f(n) is given then f(n) is O(f(n))
Symmetric Property
If f(n) is θ(g(n)) then g(n) is θ(f(n))
Transpose Property
If f(n) = O(g(n)) then g(n) is Ω(f(n))
9/11/21 105
Review Questions
• Indicate constant time complexity in terms of Big-O notation.
A. O(n)
B. O(1)
C. O(logn)
D. O(n^2)
• Big oh notation is used to describe __________
• Big O Notation is a _________function used in computer science
to describe an ____________
• Big Omega notation (Ω) provides _________bound.
• Given T1(n) =O(f(n)) and T2(n)=O(g(n).Find T1(n).T2(n)
106
ASYMPTOTIC NOTATION-
THETA
MATHEMATICAL
FUNCTIONS
Asymptotic Notation – THETA Θ
Example - THETA
• Show that n2 /2 – 2n = Θ(n2 ).
Example - THETA
Mathematical Functions
Mathematical Functions (Cont..)
Review Questions
1. Give examples of functions that are in Θ notation as well as
functions that are not in Θ notation.
2. Which function gives the positive value of the given input?
3. K (mod) M gives the reminder of ____ divided by ____
4. For a given set of number n, the number of possible
permutation will be equal to the ____________ value of n.
5. ________Notation is used to specify both the lower bound and
upper bound of the function.
DATA STRUCTURES
AND ITS TYPES
Data Structure
• A data structure is basically a group of data elements that are
put together under one name
• Defines a particular way of storing and organizing data in a
computer so that it can be used efficiently
• Data Structures are used in
∙ Compiler design
∙ Operating system
∙ Statistical analysis package
∙ DBMS
• The selection of an appropriate data structure provides the most
efficient solution
Terms in Data Structure
• Data: Data can be defined as an elementary value or the collection of values, for
example, student's name and its id are the data about the student.
• Group Items: Data items which have subordinate data items are called Group item,
for example, name of a student can have first name and the last name.
• Record: Record can be defined as the collection of various data items, for example, if
we talk about the student entity, then its name, address, course and marks can be
grouped together to form the record for the student.
• File: A File is a collection of various records of one type of entity, for example, if there
are 60 employees in the class, then there will be 20 records in the related file where
each record contains the data about each employee.
• Attribute and Entity: An entity represents the class of certain objects. it contains
various attributes. Each attribute represents the particular property of that entity.
Primitive data structures are the fundamental data types which are supported by a programming
language. Some basic data types are
∙ Integer
∙ Real
∙ Character
∙ Boolean
Non-primitive data structures are those data structures which are created using primitive data
structures. Examples of such data structures include
∙ linked lists
∙ stacks
∙ trees
∙ graphs
LINEAR AND NON-
LINEAR DATA
STRUCTURES
Types of Data Structure – Non
Primitive
Linear
Arrays Linked List
•An array is a collection of •Dynamic data structure in
similar type of data items and which elements (called nodes)
each data item is called an form a sequential list
element of the array.
Types of Data Structure – Non Primitive
Linear
Stack Queue
•Linear data structure in which •Linear data structure in which
insertion and deletion of the element that is inserted
elements are done at only one first is the first one to be taken
end, which is known as the top out
of the stack
Types of Data Structure – Non Primitive - Non
Linear
TREES
•A tree is a non-linear data structure which consists of a collection
of nodes arranged in a hierarchical order.
• One of the nodes is designated as the root node, and the
remaining nodes can be partitioned into disjoint sets such that
each set is a sub-tree of the root.
Types of Data Structure – Non Primitive - Non
Linear
GRAPHS
•A graph is a non-linear data structure which is a collection of
vertices (also called nodes) and edges that connect these vertices.
•A graph is often viewed as a generalization of the tree structure.
•every node in the graph can be connected with another node in
the graph.
•When two nodes are connected via an edge, the two nodes are
known as neighbours.
Review Questions
1. Compare a linked list with an array.
2. Is Array static structure or dynamic structure (TRUE / FALSE)
3. The data structure used in hierarchical data model is _________
4. In a stack, insertion is done at __________
5. Which Data Structure follows the LIFO fashion of working?
6. The position in a queue from which an element is deleted is
called as __________
POINTER
Definition and Features of Pointer
⮚Definition:
•Pointer is a variable that stores the address of another variable.
⮚Features
•Pointer saves the memory space.
•Execution time of pointer is faster because of direct access to
the memory location.
•With the help of pointers, the memory is accessed efficiently,
i.e., memory is allocated and deallocated dynamically.
•Pointers are used with data structures.
Pointer declaration, initialization and accessing
• Consider the following statement
int q = 159;
In memory, the variable can be represented as follows −
q Variable
159 Value
1000 Address
Pointer declaration, initialization and
accessing(Cont..)
• Declaring a pointer
It means ‘p’ is a pointer variable which holds the address of
another integer variable, as mentioned in the statement
below −
int *p;
• Initialization of a pointer
Address operator (&) is used to initialise a pointer variable.
For example −
Variable Value Address
int q = 159;
int *p;
q 159 1000
p= &q; p 1000 2000
Pointer declaration, initialization and
accessing(Cont..)
• Accessing a variable through its pointer
To access the value of a variable, indirection operator (*) is used.
For example −
int q=159, *p,n;
p=&q;
n=*p
Here, ‘*’ can be treated as value at address.
p = &q;
n = *p; n =q
1D INITIALIZATION &
ACCESSING USING POINTERS
Pointers and one-dimensional
arrays
⮚ The compiler allocates Continuous memory locations for all the
elements of the array.
⮚ The base address = first element address (index 0) of the array.
⮚ Syntax: data_type (var_name)[size_of_array] //Declaration
int *p;
• For Example − int a [5] = {10, 20,30,40,50};
int my_arr[] = {11, 22, 33,
44, 55};
⮚ Elements p = my_arr;
• The five elements are stored as follows −
Elements a[0] a[1] a[2] a[3] a[4]
Values 10 20 30 40 50
Address 1000 1004 1008 1012 1016
Base Address
a=&a[0]=1000
Pointers and one-dimensional
arrays (Cont)
• If ‘p’ is declared as integer pointer, then, an array ‘a’ can be pointed by
the following assignment −
p = a; (or) p = &a[0];
• Every value of 'a' is accessed by using p++ to move from one element
to another element. When a pointer is incremented, its value is
increased by the size of the data type that it points to. This length is
called the "scale factor".
• The relationship between ‘p’ and 'a' is explained below
P = &a[0] = 1000
P+1 = &a[1] = 1004
P+2 = &a[2] = 1008
P+3 = &a[3] = 1012
P+4 = &a[4] = 1016
Pointers and one-dimensional arrays (Cont..)
POINTER ARRAYS
Syntax: data_type (*var_name)[size_of_array]
•Example:
struct Student
{
int regNo; char name[25];
int english, science, maths;
};
struct Student stud;
scanf(“%d %s %d %d %d”,&stud.regNo, stud.name,
&stud.english,&stud.science,
printf("Register number =% &stud.maths);
d\ //Accessing member
n",stud.Regno); printf("Name Regno
%s",stud.name); //Accessing member
stud.english= stud.english +10; name
stud.science++; //Updating member
english
Nested Structures
struct Student struct Student
{ { struct m
int regNo; char name[25]; int regNo; char {
struct marks name[25]; int english; int
{ int english, science, maths; struct m marks; science;
} } } Amar; int maths;
Amar;
Amar.regNo; struct Student
};
Amar.marks.engli {
sh; int regNo; char name[25];
Amar.marks.scien struct {int english, science, maths }
ce;
marks;
Amar.marks.math
s; } Amar;
Example Program
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( )
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */