Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Data Structures Module 1

Uploaded by

shiva974053
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Data Structures Module 1

Uploaded by

shiva974053
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

MODULE

-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:

• An array is a collection of data


items stored at contiguous
memory locations.
• The idea is to store multiple
items of the same type
together.This makes it easier to
calculate the position of each
element by simply adding an
offset to a base value, i.e., the
memory location of the first
element of the array (generally
denoted by the name of the
array).
2. LINKED LISTS:

• 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

• In graph data structure, each node is called


vertex and each vertex is connected to other
vertices through edges.
Popular Graph Based Data Structures:
• Spanning Tree and Minimum Spanning Tree
• Strongly Connected Components
• Adjacency Matrix
• Adjacency List
C O M M O N DATA STRUCTURE
OPERATIONS
• Insertion: Adding an element to a data structure (e.g., adding an item to an
array).
• Deletion: Removing an element from a data structure (e.g., removing a node
from a linked list).
• Search: Finding an element within a data structure (e.g., searching for a value
in an array).
• Traversal:Visiting all elements in a data structure (e.g., traversing a tree to
process its nodes).
USE CASES

• Arrays for storing lists of items like numbers or


names.
• Linked lists for implementing a music playlist.
• Stacks for managing a web browser's back
button.
• Queues for managing print jobs in a printer
queue.
• Trees for representing the hierarchy of files and
folders in an operating system.
• Graphs for modeling social networks,
transportation systems, and more
NEED OF DATA
STRUCTURE:
• Data structure modification is
easy.
• It requires less time.
• Save storage memory space.
• Data representation is easy.
• Easy access to the large
database
APPLICATIONS OF DATA
STRUCTURES:
Data structures are used in various fields
such as:
• Operating system
• Graphics
• Computer Design
• Blockchain
• Genetics
• Image Processing
• Simulation,
• etc.
WHAT IS A N ALGORITHM?

• The word Algorithm means ” A set


of finite rules or instructions to be
followed in calculations or other
problem-solving operations ”
• Therefore Algorithm refers to a
sequence of finite steps to solve a
particular problem.
20 WHAT IS THE NEED FOR
ALGORITHMS?
1. Algorithms are necessary for solving complex problems efficiently and
effectively.
2. They help to automate processes and make them more reliable, faster,
and easier to perform.
3. Algorithms also enable computers to perform tasks that would be
difficult or impossible for humans to do manually.
4. They are used in various fields such as mathematics, computer science,
engineering, finance, and many others to optimize processes,
analyze data, make predictions, and provide solutions to
problems.
21 WHAT ARE THE CHARACTERISTICS
OF AN ALGORITHM?
2
2
• Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps
should be clear in all aspects and must lead to only one meaning.
• Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It
may or may not take input.
• Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it
should be well- defined as well. It should produce at least 1 output.
• Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time.
• Feasible: The algorithm must be simple, generic, and practical, such that it can be executed
with the available resources. It must not contain some future technology or anything.
• Language Independent: The Algorithm designed must be language-independent, i.e. it
must be just plain instructions that can be implemented in any language, and yet the output
will be the same, as expected.
2
3
• Definiteness: All instructions in an algorithm must be unambiguous, precise,
and easy to interpret. By referring to any of the instructions in an algorithm
one can clearly understand what is to be done. Every fundamental operator in
instruction must be defined without any ambiguity.
• Finiteness: An algorithm must terminate after a finite number of steps in all test
cases. Every instruction which contains a fundamental operator must be
terminated within a finite amount of time. Infinite loops or recursive functions
without base conditions do not possess finiteness.
• Effectiveness: An algorithm must be developed by using very basic,
simple, and feasible operations so that one can trace it out by using just
paper and pencil.
24 TYPES OF
ALGORITHMS:
There are several types of algorithms available. Some important algorithms are:
1. Brute Force Algorithm: It is the simplest approach to a problem.A brute force algorithm is the
first approach that comes to finding when we see a problem.
2. Recursive Algorithm: A recursive algorithm is based on recursion. In this case, a problem is broken
into several sub- parts and called the same function again and again.
3. Backtracking Algorithm: The backtracking algorithm builds the solution by searching among all
possible solutions. Using this algorithm, we keep on building the solution following criteria.Whenever a
solution fails we trace back to the failure point build on the next solution and continue this process till
we find the solution or all possible solutions are looked after.
4. Searching Algorithm: Searching algorithms are the ones that are used for searching elements
or groups of elements from a particular data structure.They can be of different types based on their
approach or the data structure in which the element should be found.
2
5
5.Sorting Algorithm: Sorting is arranging a group of data in a particular manner according to the
requirement.The algorithms which help in performing this function are called sorting algorithms. Generally
sorting algorithms are used to sort groups of data in an increasing or decreasing manner.

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

• Asymptotic notations are the mathematical notations used to describe the


running time of an algorithm when the input tends towards a particular
value or a limiting value.
• For example: In bubble sort, when the input array is already sorted, the time
taken by the algorithm is linear i.e. the best case.
• But, when the input array is in reverse condition, the algorithm takes the
maximum time (quadratic) to sort the elements i.e. the worst case.
3
2
• When the input array is neither sorted nor in reverse order, then it takes
average time. These durations are denoted using asymptotic notations.

• There are mainly three asymptotic notations:


• Big-O notation
• Omega notation
• Theta notation
07-11-
2023

33BIG-O NOTATION (O-


NOTATION)
• Big-O notation represents the upper bound of the running
time of an algorithm.Thus, it gives the worst-case
complexity of an algorithm.

• 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

42 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.

• 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.

• In computer science, space complexity is the amount of memory an algorithm needs to


solve a problem. It includes the memory used for inputs, called input space, and any
extra memory used during execution, called auxiliary space.
• Space complexity is important because it helps determine how efficient and scalable
an algorithm or data structure is. A program with a low space complexity will execute
faster because it requires less space
• Space complexity is often expressed using Big O notation, similar to time
complexity. For example, an algorithm with space complexity O(n) means that the
algorithm needs an amount of memory that's proportional to the input size, n.

• 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,

• Every element is considered as a potential match for the


key and checked for the same.

• If any element is found equal to the key, the search is


successful and the index of that element is returned.

• If no element is found equal to the key, the search yields


“No match found”.
46 FOR EXAMPLE: CONSIDER THE ARRAY ARR[] = {10,
50, 30, 70, 80,
20, 90, 40} A N D KEY = 30
• Step 1: Start from the first element (index 0) and
compare key with each element (arr[i]).
• Comparing key with first element arr[0]. SInce not
equal, the iterator moves to the next element as a
potential match.
• Comparing key with next element arr[1]. SInce not
equal, the iterator moves to the next element as a
potential match.
47

• 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

Linear Search Complexities


Time Complexity: O(n)
Linear Search Applications
1.For searching operations in smaller arrays
(<100 items).
4
9
5
0
5
1
Ashwini Data Structures with 07-11-
S C++ 2023

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

Worst Case Complexity: O(n2)


• If we want to sort in ascending order and the array is in descending order
then the worst case occurs.
Best Case Complexity: O(n)
• If the array is already sorted, then there is no need for sorting.
Average Case Complexity: O(n2)
• It occurs when the elements of the array are in jumbled order (neither
ascending nor descending).

You might also like