0% found this document useful (0 votes)
37 views

Data Structures Unit 2 SPJ

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

Data Structures Unit 2 SPJ

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

DATA STRUCTURES

SWATI JAGTAP
UNIT 2: Introduction to Data Structure &
Algorithms
 Introduction & classification of Data
structure, Introduction to the Algorithm:
Time & Space complexity of an algorithm,
Asymptotic notations, Searching: Need and
types of searching, Linear and Binary
Searching Methods, Applications, Hashing
Technique: Advanced Search Technique,
Sorting: Need and types of Sorting,
Methods: Bubble & Quick Applications
Implement the searching & sorting algorithms and analyze the computational
efficiency of the algorithms.
Classification of Data Structure
Primitive Data Structure

 These are predefined way of storing data by the system


 These are built in data structures and directly operated upon by the
machine instructions.
 In general, there are different representation on different computers.
 Integer, Floating-point number, Character constants, string constants,
pointers etc, fall in this category.
Non-Primitive Data Structure

 There are more sophisticated data structures.


 These are user defined data structures.
 The non-primitive data structures emphasize on
structuring of a group of homogeneous (same
type) or heterogeneous (different type) data
items.
UNIT 2: Introduction to Data Structure &
Algorithms
 Introduction & classification of Data
structure, Introduction to the Algorithm:
Time & Space complexity of an algorithm,
Asymptotic notations, Searching: Need and
types of searching, Linear and Binary
Searching Methods, Applications, Hashing
Technique: Advanced Search Technique,
Sorting: Need and types of Sorting,
Methods: Bubble & Quick Applications
Implement the searching & sorting algorithms and analyze the computational
efficiency of the algorithms.
Algorithm
Outline
 Algorithm
 Analysis of algo with eg:
 Types of Analysis of algorithm
 Types of time complexity
 Asymptotic notation-efficiency of algo.
 Analyzing a program’s running time
 Common program running times and their Plots
 Sub-Algorithm
Algorithm
What is algorithm?
 Is a finite set of instructions for performing a particular task.
 Instructions are nothing but the statements written in simple English Language.

Problem − Design an algorithm to add two numbers and display the result.
Analysis of algorithm
 Explain with eg. How an algorithm is analyzed?
 Algorithm analysis is an important part of a
broader computational complexity theory, which provides
theoretical estimates for the resources needed by any
algorithm which solves a given computational problem.
These estimates provide an insight into reasonable
directions of search for efficient algorithms.
 The efficiency of algorithm can be measured by
inserting a counter in the algorithm in order to count
the number of times the basic operation is executed.
 We can find the efficiency by inserting the frequency
count.
 frequency count: is count that denotes how many times
the particular statement is executed.
Algorithm Complexity

 Suppose X is an algorithm and n is the size of input data, the time and
space used by the algorithm X are the two main factors, which decide
the efficiency of X.
• Time Factor − Time is measured by counting the number of key
operations such as comparisons in the sorting algorithm.
• Space Factor − Space is measured by counting the maximum memory
space required by the algorithm.
 The complexity of an algorithm f(n) gives the running time and/or the
storage space required by the algorithm in terms of n as the size of
input data.
Types of Analysis of algorithm
 Space complexity
 How much space is required
ie, The amount of memory required by an algorithm to run to
completion.
 [Core dumps = the most often encountered cause is “memory leaks” – the
amount of memory required larger than the memory available on a given
system]

 Time complexity
 How much time does it take to run the algorithm

Often more important than space complexity


 space available (for computer programs!) tends to be larger and larger
 time is still a problem for all of us

 Often, we deal with estimates!


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.
 For example, addition of two n-bit integers takes n steps.
Consequently, the total computational time is T(n) = c ∗ n,
where c is the time taken for the addition of two bits. Here,
we observe that T(n) grows linearly as the input size
increases.
Space Complexity

 Space complexity of an algorithm represents the amount of memory space


required by the algorithm in its life cycle. The space required by an
algorithm is equal to the sum of the following two components −
• A fixed part that is a space required to store certain data and variables, that
are independent of the size of the problem. For example, simple variables
and constants used, program size, etc.
• A variable part is a space required by variables, whose size depends on
the size of the problem. For example, dynamic memory allocation,
recursion stack space, etc.
 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.
Asymptotic Analysis(Time complexity)
 Asymptotic analysis of an algorithm refers to defining the
mathematical boundation/framing of its run-time performance. Using
asymptotic analysis, we can very well conclude the best case,
average case, and worst case scenario of an algorithm.
 Asymptotic analysis refers to computing the running time of any
operation in mathematical units of computation. For example, the
running time of one operation is computed as f(n) and may be for
another operation it is computed as g(n2). This means the first
operation running time will increase linearly with the increase
in n and the running time of the second operation will increase
exponentially when n increases. Similarly, the running time of both
operations will be nearly the same if n is significantly small.
Types of time complexity
Types of time complexity based on highest order of the polynomial

1)Best case-Minimum time taken to get the o/p.

2) Average case-Avg. running time of the pgm by considering the overall


possible way.

3)Worst case-Max. Time taken to get the o/p

5 ms worst-case
4 ms

3 ms
}
average-case?
best-case
2 ms

1 ms

A B C D E F G
Input
Eg: Fn for calculating sum of n numbers in an
array.

Line 1:int sum_elements(int a[], int n)


Line 2: {
Line 3: int i, sum=0;
Line 4: for(i=0;i<n;i++)
Line 5: {
Line 6: sum=sum+ a[i];
Line 7: }
Line 8: return (sum);
Line 9: }

Note: Execution starts from for loop.


The declaration part can be neglected
Asymptotic notation-efficiency of algo.(eg:n=4)
Line 4: for(i=0;i<n;i++)
Line 5: {
Line 6: sum=sum+a[i];
Line 7: }
Line 8: return (sum);

Statement Frequency count


i=0 1 time
i<n This statement executes for (n+1) times.
condition true-n times
condition false-one time
i++ n times
Sum= sum+ a[i] n times
return (sum) 1
Total 3n+3

Neglect the constant terms- then n


In terms of the asymptotic notation, the efficiency of algorithm,
denoted as O(n)
Asymptotic Notations

 Following are the commonly used asymptotic notations to calculate the running
time complexity of an algorithm.
• Ο Notation
• Ω Notation
• θ Notation
Big Oh Notation, Ο(longest amount of time)
 The notation Ο(n) is the formal way to express the upper bound of an algorithm's running
time. It measures the worst case time complexity or the longest amount of time an
algorithm can possibly take to complete.
 given functions f(n) and g(n), we say that
f(n) =O(g(n))
if and only if there are positive constants c and n0 such that f(n)≤ c g(n) for n ≥ n0
-n=no. of i/ps or o/ps
Omega Notation (Ω)(lowest time)
 Omega notation specifically describes best case scenario. It represents the lower bound
running time complexity of an algorithm. So if we represent a complexity of an algorithm
in Omega notation, it means that the algorithm cannot be completed in less time than
this, it would at-least take the time represented by Omega notation or it can take more
(when not in best case scenario).
Theta Notation (θ)(Average)
 This notation describes both upper bound and lower bound of
an algorithm so we can say that it defines exact asymptotic
behaviour. In the real case scenario the algorithm not always
run on best and worst cases, the average running time lies
between best and worst and can be represented by the theta
notation.
Analyzing a program’s running time based on no.
of inputs
T1(n) = 40*n + 10
T2(n) = 3*n2

Running times for T1(n) and T2(n):


n T1(n) T2(n)
1 50 3
2 90 12

10 410 300

13 530 507
14 570 588
15 610 675
...

20 810 1200
21 850 1323
22 890 1452
Analyzing a program’s running time
T1(n) = 40*n + 10
T2(n) = 3*n2

If program 1 and 2 are two different methods for


finding a patient ID within the database of a small
practice with 12 patients (i.e., n = 12) which
program
would you choose?

Would your choice be different if you knew that the


practice would expand to include up to 100
patients?
Time Complexity

 Time Complexity of Linear Search Algorithm is O(n).


 Here, n is the number of elements in the linear array.
 Time Complexity of Binary Search Algorithm is O(log2n).
 Fibonacci search has an average- and worst-case complexity of O(log n)
Common program running times and their Plots
Time

Input
Size
Analyzing a program’s running time

Common program running times and their names:

Big-Oh Informal name Example


O(1) constant time
O(log n) logarithmic time binary search
O(n) linear time sequential search
O(n log n) n log n time merge sort
O(n2) quadratic time selection sort
O(n3) cubic time
O(2n) exponential time
SUBALGORITHMS

 A sub-algorithm is an algorithmic module which used or called by


some main algorithm or by some other sub-algorithm.
 The sub-algorithm performs its task and then sends back the result to
the calling algorithm
 The relationship between an algorithm and a sub-algorithm is similar
to the relationship between a main program (function) and a sub-
program (sub-function) in a programming language.
The Call to a Sub-algorithm

Algorithm Figure Sub-algorithm draw_circle


ALGORITHM Figure call
------- SUBALGORITHM draw_circle ( )
------- ------
CALL draw_circle ( ) ------
------ ------
------ ------
------ END draw_circle
END Figure return

32
UNIT 2: Introduction to Data Structure &
Algorithms
 Introduction & classification of Data
structure, Introduction to the Algorithm:
Time & Space complexity of an algorithm,
Asymptotic notations, Searching: Need and
types of searching, Linear and Binary
Searching Methods, Applications, Hashing
Technique: Advanced Search Technique,
Sorting: Need and types of Sorting,
Methods: Bubble & Quick Applications
Implement the searching & sorting algorithms and analyze the computational
efficiency of the algorithms.
Searching Method
 Information retrieval is one of the most important
applications of computers. It usually involves giving a piece
of information called the key, and ask to find a record that
contains other associated information.
 This is achieved by first going through the list to find if the
given key exists or not, a process called searching.
 Computer systems are often used to store large amounts of
data from which individual records must be retrieved
according to some search criterion.
 The process of searching for an item in a data structure
can be quit straightforward or very complex.
Searching Method
• Searching is the process of finding a given value
position in a list of values.
• It decides whether a search key is present in the
data or not.
• It is the algorithmic process of finding a particular
item in a collection of items.
• It can be done on internal data structure or on
external data structure.
Types of Searching Method

Linear or Sequential Search(sorted


and unsorted array-used for small
array)
 Binary (sorted data/large )
Linear Search
 https://www.youtube.com/watch?v=PDE8pCQ9Tz4
• Sequential search is also called as Linear Search.
• Sequential search starts at the beginning of the list
and checks every element of the list.
• It is a basic and simple search algorithm.
• Sequential search compares the element with all
the other elements given in the list. If the element
is matched, it returns the value index, else it
returns -1.
Linear Search

 It searches an element or value from an array till


the desired element or value is not found. If we
search the element 25, it will go step by step in a
sequence order. It searches in a sequence order.
Sequential search is applied on the unsorted or
unordered list when there are fewer elements in a
list.
Linear Search

 Example:
 Write a program to search for the search key entered by the user in the
following array:
(9, 4, 5, 1, 7, 78, 22, 15, 96, 45)
 You can use the linear search in this example.
Linear Search
#include <stdio.h>
#define SIZE 10

int LinearSearch(int a[ ], int key); //fn declaration

int main()
{
int a[SIZE]= {9, 4, 5, 1, 7, 78, 22, 15, 96, 45}, key, pos;

printf(“Enter the Search Key\n”);


scanf(“%d”,&key);

pos = LinearSearch(a, key); //fn call

if(pos == -1)
printf(“The search key=%d is not in the array \n”,key);
else
printf(“The search key= %d is at location=%d \n”, key, pos+1);
return 0;
}
Function LinearSearch

int LinearSearch (int a[ ], int key)


{
int i;
for (i=0; i<= SIZE-1; i++)
{
if(a[i] == key)
return i;
}
return -1;
}
Computational Complexity

 Note that the Computational Complexity of the Linear Search


is the maximum number of comparisons you need to search the
array. As you are visiting all the array elements in the worst
case, then, the number of comparisons required is:
n (n is the size of the array)
 Example:
 If a given an array of 1024 elements, then the maximum
number of comparisons required is:
n = 1024 (As many as 1024 comparisons may be required)
Types of Searching Method

Linear or Sequential Search(sorted


and unsorted array-used for small and
large array)
 Binary (sorted data/large )
Binary Search
https://www.youtube.com/watch?v=sr_bR1WwcLY

• Binary Search is used for searching an element in a


sorted array.
• It is a fast search algorithm with run-time
complexity of O(log n).
• Binary search works on the principle of divide and
conquer.
• This searching technique looks for a particular
element by comparing the middle most element of
the collection.
• It is useful when there are large number of
elements in an array.
Binary Search

 Array is sorted in ascending order. As we know binary search


is applied on sorted lists only for fast searching.

For example, if searching an element


25 in the 7-element array, following
figure shows how binary search
works:
Binary Search

 Given a sorted array Binary Search algorithm can be used


to perform fast searching of a search key on the sorted
array.
 The following program uses pointer notation to implement
the binary search algorithm for the search key entered by
the user in the following array:
(3, 5, 9, 11, 15, 17, 22, 25, 37, 68)
Binary Search
#include <stdio.h>
#define SIZE 10
int BinarySearch(int [ ], int);
int main()
{
int a[SIZE]= {3, 5, 9, 11, 15, 17, 22, 25, 37, 68}, key, pos;
printf(“Enter the Search Key\n”);
scanf(“%d”,&key);
pos = BinarySearch(a, key);
if(pos == -1)
printf(“The search key is not in the array\n”);
else
printf(“The search key %d is at location %d\n”, key, pos+1);
return 0;
}
int BinarySearch (int A[], int key)
int BinarySearch (int a[ ], int key)
{
int low=0,high=SIZE-1,middle;

while(low <= high)


{
middle= (low + high)/2;

if(key == a[middle])
return middle;

else if(key <a[middle])


high = middle-1;
else
low = middle+1;
}
return -1;
}
Computational Complexity
 The Computational Complexity of the Binary Search
algorithm is measured by the maximum (worst case)
number of Comparisons it performs for searching
operations.
 The searched array is divided by 2 for each
comparison/iteration.
 Therefore, the maximum number of comparisons is
measured by:
 log2n where n is the size of the array

 Example:
 If a given sorted array 1024 elements, then the
maximum number of comparisons required is:
 log2(1024) = 10 (only 10 comparisons is enough)
Comparison of linear &Binary search
Sr. Parameter Linear /Sequential Search Binary Search
No.
1 Definition Key element is compared with Array is divided into
each element of the array one two.Key element is
by one(ie, sequentially) compared with the
middle element. If
greater, then search in
the sublist. Else
search in the first
sublist.
2 Condition/ No need for Array/elements to Array should be sorted
Prerequisite be sorted . .

3 No of Very high for more no. of Very less for more no.
comparisons datas of data
required if 2ˆ10 comparisons log2(1024) = 10 (only
n=1024 10 comparisons is
= 2ˆ10 enough) 51
Comparison of linear &Binary search

Sr. Parameter Linear /Sequential Search Binary Search


No
.
4 Time Best case: O(1) Best case: O(1)
Complexity: Worst case: O(n) Worst case: O(log2n)

5 Performance Works better for small n and Works better for large
slow for large n n

6 Application Can be used when the list is Can be used when the
dynamic(ie, changing) list is not dynamic(ie,
fixed)

52
Some real life applications of Binary Search

 Dictionary
 Debugging a linear piece of code
 Figuring out resource requirements for a large system
UNIT 2: Introduction to Data Structure &
Algorithms
 Introduction & classification of Data
structure, Introduction to the Algorithm:
Time & Space complexity of an algorithm,
Asymptotic notations, Searching: Need and
types of searching, Linear and Binary
Searching Methods, Applications, Hashing
Technique: Advanced Search Technique,
Sorting: Need and types of Sorting,
Methods: Bubble & Quick Applications
Implement the searching & sorting algorithms and analyze the computational
efficiency of the algorithms.
Hashing in Data Structure-
 In data structures,
 Hashing is a well-known technique to search any particular
element among several elements.
 It minimizes the number of comparisons while performing the
search.

 Advantage-

 Unlike other searching techniques,Hashing is extremely
efficient.
 The time taken by it to perform the search does not depend
upon the total number of elements.
 It completes the search with constant time complexity O(1).

Introduction to Hashing Function in C
 Hashing is a technique with faster access to elements
that maps the given data with a lesser key for
comparisons. In general, in this technique, the keys are
traced using hash function into a table known as the
hash table.
 What is Hash Function?
 The hash function is a function that uses the constant-
time operation to store and retrieve the value from the
hash table, which is applied on the keys as integers and
this is used as the address for values in the hash table.
Hashing Mechanism-
 In hashing, An array data structure called as Hash table is used to store the
data items.
 Based on the hash key value, data items are inserted into the hash table.

 Hash Key Value-


 Hash key value is a special value that serves as an index for a data item.
 It indicates where the data item should be be stored in the hash table.
 Hash key value is generated using a hash function.
Types of a Hash Function In C

 1. Division method
 2. Mid Square Method
 3. Digit Folding Method
Division method
 In this method, the hash function is dependent upon the remainder of a
division.
 Example: elements to be placed in a hash table are 42,78,89,64 and let’s take
table size as 10.
 Hash (key) = Elements % table size;
 2 = 42 % 10;
 8 = 78 % 10;
 9 = 89 % 10;
 4 = 64 % 10;
Mid Square Method
 In this method, the middle part of the squared element is taken as the
index.
 Element to be placed in the hash table are 210, 350, 99, 890 and the
size of the table be 100.
 210* 210 = 44100, index = 1 as the middle part of the result (44100) is
1.
 350* 350 = 122500, index = 25 as the middle part of the result
(122500) is 25.
 99* 99 = 9801, index = 80 as the middle part of the result (9801) is 80.
 890* 890 = 792100, index = 21 as the middle part of the result
(792100) is 21.
Hashing Data Structure(Advanced
searching)
 Hashing is a technique or process of mapping keys, values into the hash table
by using a hash function. It is done for faster access to elements. The
efficiency of mapping depends on the efficiency of the hash function used.
 Let a hash function H(x) maps the value at the index x%10 in an Array. For
example if the list of values is [11,12,13,14,15] it will be stored at positions
{1,2,3,4,5} in the array or Hash table respectively.
Applications of Hashing
 Hashing provides constant time search, insert and delete operations on
average. This is why hashing is one of the most used data structure, example
problems are, distinct elements, counting frequencies of items, finding
duplicates, etc.
 There are many other applications of hashing, including modern day
cryptography hash functions. Some of these applications are listed below:

 Message Digest
 Password Verification
 Data Structures(Programming Languages)
 Compiler Operation
 Rabin-Karp Algorithm
 Linking File name and path together
Binary Search
https://www.youtube.com/watch?v=sr_bR1WwcLY

• Binary Search is used for searching an element in a


sorted array.
• It is a fast search algorithm with run-time
complexity of O(log n).
• Binary search works on the principle of divide and
conquer.
• This searching technique looks for a particular
element by comparing the middle most element of
the collection.
• It is useful when there are large number of
elements in an array.
Binary Search

 Array is sorted in ascending order. As we know binary search


is applied on sorted lists only for fast searching.

For example, if searching an element


25 in the 7-element array, following
figure shows how binary search
works:
Binary Search

 Given a sorted array Binary Search algorithm can be used


to perform fast searching of a search key on the sorted
array.
 The following program uses pointer notation to implement
the binary search algorithm for the search key entered by
the user in the following array:
(3, 5, 9, 11, 15, 17, 22, 25, 37, 68)
Binary Search
#include <stdio.h>
#define SIZE 10
int BinarySearch(int [ ], int);
int main()
{
int a[SIZE]= {3, 5, 9, 11, 15, 17, 22, 25, 37, 68}, key, pos;
printf(“Enter the Search Key\n”);
scanf(“%d”,&key);
pos = BinarySearch(a, key);
if(pos == -1)
printf(“The search key is not in the array\n”);
else
printf(“The search key %d is at location %d\n”, key, pos+1);
return 0;
}
int BinarySearch (int A[], int key)
int BinarySearch (int a[ ], int key)
{
int low=0,high=SIZE-1,middle;

while(low <= high)


{
middle= (low + high)/2;

if(key == a[middle])
return middle;

else if(key <a[middle])


high = middle-1;
else
low = middle+1;
}
return -1;
}
Computational Complexity
 The Computational Complexity of the Binary Search
algorithm is measured by the maximum (worst case)
number of Comparisons it performs for searching
operations.
 The searched array is divided by 2 for each
comparison/iteration.
 Therefore, the maximum number of comparisons is
measured by:
 log2n where n is the size of the array

 Example:
 If a given sorted array 1024 elements, then the
maximum number of comparisons required is:
 log2(1024) = 10 (only 10 comparisons is enough)
Fibonacci Search:-Algorithm
 Let the searched element be x.
 The idea is to first find the smallest Fibonacci number that is greater than or equal to the length of given
array. Let the found Fibonacci number be fib (m’th Fibonacci number). We use (m-2)’th Fibonacci number
as the index (If it is a valid index). Let (m-2)’th Fibonacci Number be i, we compare arr[i] with x, if x is
same, we return i. Else if x is greater, we recur for subarray after i, else we recur for subarray before i.
 Below is the complete algorithm
Let arr[0..n-1] be the input array and element to be searched be x.
1. Find the smallest Fibonacci Number greater than or equal to n. Let this number be fibM [m’th Fibonacci
Number]. Let the two Fibonacci numbers preceding it be fibMm1 [(m-1)’th Fibonacci Number] and
fibMm2 [(m-2)’th Fibonacci Number].
2. While the array has elements to be inspected:
1. Compare x with the last element of the range covered by fibMm2
2. If x matches, return index
3. Else If x is less than the element, move the three Fibonacci variables two Fibonacci down, indicating elimination of
approximately rear two-third of the remaining array.
4. Else x is greater than the element, move the three Fibonacci variables one Fibonacci down. Reset offset to index.
Together these indicate elimination of approximately front one-third of the remaining array.
3. Since there might be a single element remaining for comparison, check if fibMm1 is 1. If Yes, compare x
with that remaining element. If match, return index.
Syllabus
Unit II Searching and Sorting Algorithms (06 Hrs)

Algorithms: Analysis of Iterative and Recursive


algorithms, Space & Time complexity, Asymptotic
notation- Big-O, Theta and Omega notations.
Searching methods: Linear, Binary and Fibonacci
Search.

Sorting methods: Bubble, Insertion, Selection, Merge,


and Quick Sort.
Mapping of Course Outcomes for Unit II
CO2: Implement sorting and searching algorithms and calculate their
complexity.

You might also like