Data Structures Unit 2 SPJ
Data Structures Unit 2 SPJ
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
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
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.
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
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
Input
Size
Analyzing a program’s running time
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
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 main()
{
int a[SIZE]= {9, 4, 5, 1, 7, 78, 22, 15, 96, 45}, key, pos;
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
if(key == a[middle])
return middle;
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
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 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
if(key == a[middle])
return middle;
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)