Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 46

Data Structures and Algorithms

Presented by
D.Ranjith
Asst Prof
UNIT-1
Basic Concepts-Introduction
• Handling Arrays
• Data structures and Types
• Abstract Data Type(ADT)
• Revision of Recursion-Tower of Hanoi
• Solving Recurrence Relations
Searching
• Linear Search
• Binary Search
Unit-1
Time Complexity
• Asymptotic Analysis
• Big-Oh Notation
Sorting
• Bubble sort
• Insertion sort
• Selection sort
• Quick sort
• Merge sort
• Comparison of Sorting methods
WHAT IS DATA
• Data is the collection of different numbers,
symbols, and alphabets to represent
information.
Structure:
• The action of building: Construction
• Something(such as building) that is
constructed
What is Data Structure?
• Whenever we want to work with a large
amount of data, then organizing that data is
very important.
• If that data is not organized effectively, it is
very difficult to perform any task on that data.
• If it is organized effectively then any operation
can be performed easily on that data.
Data Structure
• Data structure is a method of organizing a
large amount of data more efficiently so that
any operation on that data becomes easy
Note :
1. Every data structure is used to organize the large
amount of data
2. Every data structure follows a particular principle
3. The operations in data structure should not
violate the basic principle of that data structure.
Continue...
• Based on the organizing method of data
structure, data structures are divided into two
types.
• Linear Data Structures
• Non - Linear Data Structures
Linear Data Structures
• If a data structure organizes the data in
sequential order, then that data structure is
called a Linear Data Structure.
Example
• Arrays
• List (Linked List)
• Stack
• Queue
Non - Linear Data Structures
• If a data structure organizes the data in
random order, then that data structure is called
as Non-Linear Data Structure.
Example
• Tree
• Graph
• Dictionaries
• Heaps
• Tries, Etc.,
Data structures are used in various fields
such as
• Operating system
• Graphics
• Computer Design
• Block chain
• Genetics
• Image Processing
• Simulation etc.
Advantages of Data structures
1. Efficiency: Proper choice of data structures
make program efficient in terms of space and
time.
2. Reusability: One implementation can be
used by multiple client programs.
3. Abstraction: Data structure is specified by
an ADT which provides a level of abstraction,
The client program doesn’t worry about the
implementation details.
Data type Vs Abstract data type(ADT)
• What is Data Type?
1. Defines a certain domains of values.
2. Defines operations allowed on those values.
Example:
Int type:
--Takes only integer values.
--Operations: Addition, Subtraction, Multiplications, bitwise
operations etc.
Float type:
--Takes only floating values.
--Operations: Addition, Subtraction, Multiplication, Division
etc(Bitwise and modulus operations are not allowed)
User Defined Data types
• The operations and values of user defined data types
are not specified in the language itself but it is specified
by the user.
Ex: Structure, Union and Enumeration
--By using structures, we are defining our own type by
combining other data types.
struct book{
int price;
char author;
}
Abstract Data Type(ADT)
• ADT’s are like user defined data types which
defines operations on values using functions
without specifying what is there inside the
function and how the operations are
performed.
Ex: Stack ADT.
--A stack consists of elements of same type
arranged in a sequential order.
Operations:
• Initialize()—Initializing it to be empty.
• Push()– Insert an element into the stack.
• Pop()– Delete an element from the stack.
• isEmpty()– Check if stack is empty.
• isFull()– Check if stack is full.
ADT
• Think of ADT as a black box which hides the
inner structure and design of the data type
from the user.
• There are multiple ways to implement an ADT.
EX:
--A stack ADT can be implemented using arrays
or linked lists.
Performance Analysis
• Performance analysis helps us to select the best algorithm
from multiple algorithms to solve a problem.
• Performance analysis of an algorithm is the process of
calculating space and time required by that algorithm.
• Performance analysis of an algorithm is performed by
using the following measures...
1. Space required to complete the task of that algorithm
(Space Complexity). It includes program space and data
space
2. Time required to complete the task of that algorithm (Time
Complexity)
Space complexity
• Total amount of computer memory required by an algorithm
to complete its execution is called as space complexity of
that algorithm
• Generally, when a program is under execution it uses the
computer memory for THREE reasons. They are as follows...
1. Instruction Space: It is the amount of memory used to store
compiled version of instructions.
2. Environmental Stack: It is the amount of memory used to
store information of partially executed functions at the time
of function call.
3. Data Space: It is the amount of memory used to store all the
variables and constants.
Time complexity
• The time complexity of an algorithm is the total amount of time
required by an algorithm to complete its execution.
• Generally, the running time of an algorithm depends upon the
following...
1. Whether it is running on Single processor machine
or Multi processor machine.
2. Whether it is a 32 bit machine or 64 bit machine.
3. Read and Write speed of the machine.
4. The amount of time required by an algorithm to
perform Arithmetic operations, logical operations, return value
and assignment operations etc.,
5. Input data.
Asymptotic Notation
• Whenever we want to perform analysis of an algorithm,
we need to calculate the complexity of that algorithm.
• But when we calculate the complexity of an algorithm it
does not provide the exact amount of resource required.
• So instead of taking the exact amount of resource, we
represent that complexity in a general form (Notation)
which produces the basic nature of that algorithm.
• We use that general form (Notation) for analysis process.
Asymptotic notation
• Asymptotic notation of an algorithm is a
mathematical representation of its complexity.
• We use THREE types of Asymptotic Notations
and those are as follows...
1.Big - Oh (O)
2.Big - Omega (Ω)
3.Big - Theta (Θ)
Big - Oh Notation (O)
• Big - Oh notation is used to define the upper
bound of an algorithm in terms of Time
Complexity.
• That means Big - Oh notation always indicates
the maximum time required by an algorithm
for all input values.
• That means Big - Oh notation describes the
worst case of an algorithm time complexity.
Big Oh
• Big - Oh Notation can be defined as follows...
• Consider function f(n) as time complexity of
an algorithm and g(n) is the most significant
term.
• If f(n) <= C g(n) for all n >= n0, C > 0 and
n0 >= 1.
• Then we can represent f(n) = O(g(n)).
Big-Oh
• Consider the following graph drawn for the values of
f(n) and C g(n) for input (n) value on X-Axis and
time required is on Y-Axis.
• In below graph after a particular input value n 0,
always C g(n) is greater than f(n) which indicates the
algorithm's upper bound.
Example
• Consider the following f(n) and g(n)...
f(n) = 3n + 2 g(n) = n
If we want to represent f(n) as O(g(n)) then it must satisfy f(n)
<= C g(n) for all values of
• C > 0 and n0>= 1
f(n) <= C g(n)
⇒3n + 2 <= C n
Above condition is always TRUE for all values of
• C = 4 and n >= 2.
• By using Big - Oh notation we can represent the time
complexity as follows...
3n + 2 = O(n)
Recurrence Relation
• A recurrence relation is an equation which
represents a sequence based on some rule.
• It helps in finding the subsequent term (next
term) dependent upon the preceding term
(previous term).
• If we know the previous term in a given series,
then we can easily determine the next term.
• Since a standard pattern is developed now, we
can find the set of new terms.
Recurrence Relation Formula
• Let us assume xn is the nth term of the series.
Then the recurrence relation is shown in the
form of:
• xn + 1 = f(xn) ; n>0
• Where f(xn) is the function.
Recurrence Relation Formula
• We can also define a recurrence relation as an
expression that represents each element of a
series as a function of the preceding ones.
xn= f(n,xn-1) ; n>0

• To write the recurrence relation of first-order, say


order k, the above formula can be represented as:
• xn = f(n, xn-1 , xn-2 , ……, xn-k) ; n-k>0
Examples of Recurrence Relation

Factorial Representation
• We can define the factorial by using the concept
of recurrence relation, such as;
• n!=n(n-1)! ; n>0
• When n = 0,
• 0! = 1 is the initial condition.
• To find the further values we have to expand the
factorial notation, where the succeeding term is
dependent on the preceding one.
Fibonacci Numbers
• In Fibonacci numbers or series, the succeeding
terms are dependent on the last two preceding
terms.
• Fn = Fn-1 + Fn-2
Now, if we take the initial values;
• F0 = 0 and F1 = 1
• So, F2 = F1 + F0 = 0 + 1 = 1
Fibonacci Numbers
• In the same way, we can find the next
succeeding terms, such as;
• F3 = F2 + F1
• F4 = F3 + F2
• And so on.
• Thus, the Fibonacci series is given by;
• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …∞
Tower of Hanoi
• Tower of Hanoi is a mathematical puzzle
where we have 3 towers and ‘n’ discs.
• The objective of the puzzle is to move the
entire stack to another tower.
Tower of Hanoi Rules:
1. Only one disc can be moved at a time.
2. A disc can only be moved if it is the upper
most disc on a stack.
3. No disk may be placed on top of a smaller
disc.
Solution for single disc:
• Move a disc from A to C
Solution for 2 Disc:
• Move a disc from A to B using C.
• Move a disc from A to C.
• Move a disc from B to C using A.
Solution for 3 disc:
• Move 2 discs from A to B using C.
• Move a disc from A to C.
• Move 2 discs from B to C using A.
Solution for N Disc:
• Move N-1 discs from A to B using C.
• Move a disc from A to C.
• Move N-1 discs from B to C using A.
Recursive Algorithm:
Void TOH(int n ,int A, int B, int C)
{
if(n>0)
{
TOH(n-1,A,C,B);
printf(“move a disc from %d to
%d”,A,C);
TOH(n-1,B,A,C);
}
}
Analysis of different sorting techniques

• Comparison based sorting –


In comparison based sorting, elements of an array are
compared with each other to find the sorted array.
• Bubble sort
• Insertion sort
• Selection sort
• Merge sort
• Heap sort
• Quick sort
Non-comparison based sorting
• In non-comparison based sorting, elements of
array are not compared with each other to find
the sorted array.
• Radix sort
• Count sort
• Bucket sort
Properties:
1.In-place/Outplace technique –
A sorting technique is in place if it does not use
any extra memory to sort the array.
• Among the comparison based techniques
discussed, only merge sort is outplaced
technique as it requires an extra array to merge
the sorted subarrays.
2.Online/Offline technique –
• A sorting technique is considered Online if it can
accept new data while the procedure is ongoing i.e.
complete data is not required to start the sorting
operation.
• Among the comparison based techniques discussed,
only Insertion Sort qualifies for this because of the
underlying algorithm it uses i.e. it processes the
array (not just elements) from left to right and if
new elements are added to the right, it doesn’t
impact the ongoing operation
3.Stable/Unstable technique –
• A sorting technique is stable if it does not change the order
of elements with the same value.
• Out of comparison based techniques, bubble sort, insertion
sort and merge sort are stable techniques.
• Selection sort is unstable as it may change the order of
elements with the same value. For example, consider the
array 4, 4, 1, 3.
• In the first iteration, the minimum element found is 1 and it
is swapped with 4 at 0th position. Therefore, the order of 4
with respect to 4 at the 1st position will change. Similarly,
quick sort and heap sort are also unstable.
Comparison of Sorting Techniques:
Type of Time Complexity Space Online Stable In place
Sorting Complexity
Best Average Worst

Bubble O(n) O(n2) O(n2) O(1) No Yes Yes


Sort

Selection O(n2) O(n2) O(n2) O(1) No No Yes


Sort(min)

Insertion O(n) O(n2) O(n2) O(1) Yes Yes Yes


Sort

Merge O(nlogn) O(nlogn) O(nlogn) O(n) No Yes No


Sort

Quick O(n O(nlogn) O(n2) O(1) No No Yes


logn)
Sort
Analysis of sorting techniques :
• When the array is almost sorted, insertion sort
can be preferred.
• When order of input is not known, merge sort
is preferred as it has worst case time
complexity of nlogn and it is stable as well.
• When the array is sorted, insertion and bubble
sort gives complexity of n but quick sort gives
complexity of n^2.
• Thank you...

You might also like