Data Structures Module 1
Data Structures Module 1
-I
INTRODUCTION TO DATA STRUCTURES,
ALGORITHMS, SEARCHING &
SORTING
2
AGENDA
what is data structure?
Classification/types of DS
Common Data Structure
Operations
Use Cases
Applications
3 WHAT IS DATA
STRUCTURE?
• A data structure is a particular way
of organizing data in a computer so
that it can be used effectively.The
idea is to reduce the space and time
complexities of different tasks.
4 CLASSIFICATION/TYPES OF DATA
STRUCTURES:
1. Linear Data Structure
2. Non-Linear Data Structure.
Linear Data Structure:
• Data structure in which data elements are arranged sequentially or linearly,
where each element is attached to its previous and next adjacent elements,
is called a linear data structure.
• Elements are arranged in one dimension ,also known as linear dimension.
• Example: lists, stack, queue, etc.
Non-Linear Data Structure:
• Data structures where data elements are not placed sequentially or
linearly are called non-linear data structures. In a non-linear data
structure, we can’t traverse all the elements in a single run only.
• Elements are arranged in one-many, many-one and many-many
dimensions.
• Example: tree, graph, table, etc.
1. ARRAY:
• Like arrays, Linked List is a linear data structure. Unlike arrays, linked list
elements are not stored at a contiguous location; the elements are linked
using pointers.
3.
STACK:
• Stack is a linear data structure which follows a particular order in which the operations
are performed.
• The order may be LIFO(Last In First Out) or FILO(First In Last Out). In stack, all insertion
and deletion are permitted at only one end of the list.
STACK
OPERATIONS:
• push(): When this operation is
performed, an element is inserted into
the stack.
• pop(): When this operation is performed, an
element is removed from the top of the stack
and is returned.
• top(): This operation will return the last
inserted element that is at the top without
removing it.
• size(): This operation will return the size of
the stack
i.e. the total number of elements present in the
stack.
• isEmpty(): This operation indicates
whether the stack is empty or not.
4.
QUEUE:
• Like Stack, Queue is a linear structure which
follows a particular order in which the
operations are performed.
• The order is First In First Out (FIFO). In the
queue, items are inserted at one end and
deleted from the other end.
• A good example of the queue is any queue
of consumers for a resource where the
consumer that came first is served first.
QUEUE
OPERATIONS:
• Enqueue(): Adds (or stores) an element to the
end of the queue..
• Dequeue(): Removal of elements from the
queue.
• Peek() or front(): Acquires the data element
available at the front node of the queue without
deleting it.
• rear(): This operation returns the element at the
rear end without removing it.
• isFull(): Validates if the queue is full.
• isNull(): Checks if the queue is empty.
TREES DATA
STRUCTURE
• A tree is also a collection of vertices and edges.
However, in tree data structure, there can only be one
edge between two vertices.
Popular Tree based Data Structure
• Binary Tree
• Binary Search Tree
• AVL Tree
• B-Tree
• B+ Tree
• Red-Black Tree
GRAPH DATA STRUCTURE
6.Hashing Algorithm: Hashing algorithms work similarly to the searching algorithm. But they contain
an index with a key ID. In hashing, a key is assigned to specific data.
7.Divide and Conquer Algorithm: This algorithm breaks a problem into sub-problems,
solves a single sub- problem, and merges the solutions to get the final solution. It consists of the
following three steps: Divide, Solve, Combine
8.Greedy Algorithm: In this type of algorithm, the solution is built part by part.The solution for the
next part is built based on the immediate benefit of the next part.The one solution that gives the most
benefit will be chosen as the solution for the next part.
26 DESIGNING THE
ALGORITHM
• Algorithm to add 3 numbers and print their sum:
1.START
2.Declare 3 integer variables num1, num2, and num3.
3.Take the three numbers, to be added, as inputs in variables num1, num2,
and num3 respectively.
4.Declare an integer variable sum to store the resultant sum of the 3
numbers.
5.Add the 3 numbers and store the result in the variable sum.
6.Print the value of the
variable sum 7.END
#include <iostream cout < <”Enter the 3rd number
using namespace ”;
std: int main() c1n >> num3 ;
cout << " << nun3 ;
// Variables co rake input of / / Ca l c u1 a t t o e s u u s in • o p e i ar o
the
// the 3 nu;bers e and s t or e i‘
int num1 num2, num3 ; // num1 + i t 1n va r 1ab T e su»
sum num2 + num3 ;
// Variable to sio -e the resultant
/ / P i- in I he s uili
sun t
int sum: cout <° " \ nsum of the 3 number s is "
// Take t he 3 nut be i- s < < SUITI j
as input cout << Ente the 1st number 3
return r
"Enter the 1st number c1n >>
0,
num1 Enter the 2nd number
cout << << num1 << 4 4
cout <<"Enter the 2nd numbe "
endl: Ente the 3rd number
c1n >> nun2; r ; 6
r 6
cout < < " " nun2 < end1 ;
Sum of the 3 is
<< <
numbers 13
2
8
29 PERFORMANCE
ANALYSIS
• When several algorithms can be designed for the solution of a problem,
there arises the need to determine which among them is the best.
• The efficiency of a program or an algorithm is measured by computing its time
and/or space complexities.
• The time complexity of an algorithm is a function of the running time of the
algorithm.
• The space complexity is a function of the space required by it to run to
completion.The time complexity is therefore given in terms of frequency
count.
• Frequency count is basically a count denoting number of times a statement
execution
30 ASYMPTOTIC
ANALYSIS
• The efficiency of an algorithm depends on the amount of time, storage and
other resources required to execute the algorithm.
• The efficiency is measured with the help of asymptotic notations.
• An algorithm may not have the same performance for different types of
inputs.With the increase in the input size, the performance will change.
• The study of change in performance of the algorithm with the change in the
order of the input size is defined as asymptotic analysis.
31 ASYMPTOTIC NOTATIONS
• The above expression can be described as a function f(n) belongs to the set O(g(n)) if there exists a
positive constant c such that it lies between 0 and cg(n), for sufficiently large n.
• For any value of n, the running time of an algorithm does not cross the time provided by O(g(n)).
• Since it gives the worst-case running time of an algorithm, it is widely used to analyze an algorithm
as we are always interested in the worst-case scenario.
3
4
07-11-
2023
41 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 above expression can be described as a function f(n) belongs to the set
Ω(g(n)) if there exists a positive constant c such that it lies above cg(n), for
sufficiently large n.
• For any value of n, the minimum time required by the algorithm is given by
Omega Ω(g(n)).
07-11-
2023
• The above expression can be described as a function f(n) belongs to the set Θ(g(n)) if there exist positive
constants c1 and c2 such that it can be sandwiched between c1g(n) and c2g(n), for sufficiently large n.
• If a function f(n) lies anywhere in between c1g(n) and c2g(n) for all n ≥ n0, then f(n) is said to be
asymptotically tight bound.
43 SPACE
COMPLEXITY
• The space complexity can be defined as amount of memory required by an algorithm
to run.
• The space Complexity of an algorithm is the total space taken by the algorithm with
respect to the input size. Space complexity includes both Auxiliary space and space
used by input.
• Let p be an algorithm, To compute the space complexity we use two factors: constant and
instance characteristics. The space requirement S(p) can be given as
• S(p) = C + Sp
• where C is a constant i.e.. fixed part and it denotes the space of inputs and outputs.
• This space is an amount of space taken by instruction, variables and identifiers.
• Sp is a space dependent upon instance characteristics.
• This is a variable part whosespace requirement depend on particular problem
4
4
EX 1:
Algorithm
add(a,b,c)
{
return
a+b+c;
}
•If we assume a, b, c occupy one word size then total size comes to
be 3 S(p) = C EX 2: Algorithm
add(x,n)
{
} S(p) ≥ (n+3)
sum=0.0;
for i= 1 to n do sum:=sum+x[i];return sum;
• The n space required for x[], one space for n, one for i, and one for
sum
45 LINEAR SEARCH
• Linear Search is defined as a sequential search algorithm that starts at one end and goes
through each element of a list until the desired element is found, otherwise the search
continues till the end of the data set.
How Does Linear Search Algorithm Work?
In Linear Search Algorithm,
• Step 2: Now when comparing arr[2] with key, the value matches. So the Linear
Search Algorithm will yield a successful message and return the index of the
element when key is found (here 2).
Ashwini S Data Structures with C++
07-11-2023
48
5
2
Data Structures with 07-11-
C++ 2023
5
3
’/*recursive function for binaiy search*/
int rbinary_search(int list[ ],int key,int low,int high)
{
int mid,pos=- l ;
if(low<=high)
mid=(low+high)/2;
if(key==list[mid])
pos=mid;
return pos;
else
if(key>list[mid])
return rbinary
search(list,ke
y,low,mid-l);
else
return
rbinary
search(list,
56 BUBBLE SORT
• Bubble sort is a sorting algorithm that compares two adjacent elements and
swaps them until they are in the intended order.
• Just like the movement of air bubbles in the water that rise up to the
surface, each element of the array move to the end in each
iteration.Therefore, it is called a bubble sort.
571. FIRST ITERATION (COMPARE A N D
SWAP)
1. Starting from the first index,
compare the first and the second
elements.
2. If the first element is greater
than the second element, they
are swapped.
3. Now, compare the second and the
third elements. Swap them if they
are not in order.
4. The above process goes on until
the last element.
07-11-
2023
58 2. REMAINING
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.
• In each iteration, the comparison takes
place up to the last unsorted
element.
5
9
• The array is sorted when all the unsorted elements are placed at their correct positions.
60 BUBBLE SORT
ALGORITHM
Data Structures with 07-1-
C++ 2023
6
1
6
2
63BUBBLE SORT
COMPLEXITY
6
4
• Hence, the number of
comparisons is
• nearly equals to n2
• Hence, Complexity: O(n2)
Also, if we observe the code, bubble sort requires two loops. Hence, the
complexity is n*n = n2
TIME COMPLEXITIES