SlideShare a Scribd company logo
1 of 23
SEARCHING
Dictionary as an ADT
 A dictionary is an abstract data type that defines an unordered collection of data as a set of key-value pairs.
 Each key, which must be unique, has an associated value.
 Typically, the keys in a dictionary must be simple types (such as integers or strings) while the values can be of any type.
 Standard dictionary operations include:
retrieve a value associated with a particular key
insert a new key-value pair into the collection
remove a key-value pair from the collection
update a value associated with a key
 Using a key allows direct access to items stored within the collection. This provides much quicker access to unordered data
than searching through the data set using a linear search and will also outperform a binary search. Key-value retrieval is a
technique widely used in various high-performance systems, such as caches and databases.
Defining a dictionary
 Many languages provide a dictionary (sometimes called an associative array) as a built-in data type; in others we may
find a library that includes an appropriate implementation that we can use.
 Different languages enforce different type restrictions on keys and values in a dictionary.
 Dictionaries are often implemented as hash tables.
 Here we will consider the use of a built-in type and present pseudocode for main operation.
DICTIONARY results = {"Detra" : 17, "Nova" : 84,"Charlie" : 22, "Hwa" : 75, "Roxan" : 92,"Elsa" : 29, "Barbara": Null}
 Here a dictionary has been defined in pseudocode. The identifier (name) of the dictionary is results.
 The key is the name of the student, and the value is the result of a recent assessment.
 This is a simplistic example to demonstrate the main principles.
 The use of a student's first name as a key is unlikely to be a sensible choice as we would probably have more than one
student with the same name (and key values must be unique).
 Note that a dictionary is unordered. No attempt should be made to keep the data in any kind of order, as it is arranged to
optimize the speed of retrieving a value by key rather than by position.
Algorithmic Notation
 Algorithm notations are used to compare efficiency between different algorithms, the basics are:
 o(n) , O(n) , Θ(n) , Ω(n) , ω(n) ,:- faster , faster or equal , equal to , slower or equal to , slower than,
o(n) - faster , O(n) - faster or equal , Θ(n) - equal to, Ω(n) - slower or equal to , ω(n) - slower than
 We want to pay extra attention to O(n)O(n) since it is either faster or equal, which takes the worst case scenario into account,
which is equal efficiency.
 Now that we know this, we might have a program:
Load: → O(n2)O(n2)
Execute part 1: → O(nlogn)O(nlogn)
Execute part 2: → O(n)O(n)
Save: → O(1)O(1)
Then you can add up all of these: O(n2) + O(nlogn) + O(n) + O(1)+ O(n2) + O(nlogn) + O(n) + O(1) to calculate the speed of
the program. In this case, n2n2is the worst case scenario.
 There are basically 3 types of asymptotic notations-
Big O
Omega (also called Big Omega)
Theta (also called Big Theta)
 The above 3 asymptotic notations are used for measuring the time complexity of an algorithm.
 The time complexity of a program statement is nothing but the number of times it gets executed.
For example,
1. The following code snippet will execute only once if it’s get compiled on a machine. Hence, its time complexity will be
O (1).
2. The following for loop has executed n times where n = 10. Here, whatever value we substitute for n, it will run for that number
of times. Hence, its time complexity will be O (n).
int n=10,count=0;
for(i=0;i<n;i++)
count=count+1;
printf(“%d”,count);
3. The following code has executed n*n times which is clear from its logic.
This code snippet will have a complexity of O (n^2).
4. The following code snippet will give an output corresponding to the input in such a manner that--
Output = (input * (input + 1))/2
This is nothing but n(n+1) / 2 where n is the input
More precisely, n (n+1) / 2 = (n^2 + n) /2
We can approximately take this value as n^2.
Hence, here also time complexity will be O(n^2)
int n=10,count=0;
for(i=0;i<n;i++)
{
for(j=0;j<i+1;j++)
count=count+1;
}
printf(“%d”,count);
int n=5,count=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
count=count+1;
}
printf(“%d”,count);
 Big O is nothing but a notation to specify the upper bound of an algorithm’s time complexity.
 Omega is nothing but a notation to specify the lower bound of an algorithm’s time complexity.
 Theta is nothing but a notation to specify the exact bound of an algorithm’s time complexity.
Any algorithm’s time complexity will be any one of the below list or at least multiple of any of these elements-
 1
 log n
 sqrt(N)
 n
 n log n
 n^2
 n^3
 2^n
 3^n
 n ^ n
 Big O
The function f (n) = O (g (n)) if there exists positive constants C and n0 such that f(n) <= C*g(n) where n > n0
For example,
Let f(n) = 3n + 5
Then, we can write that –
3n + 5 < 10n (Here, C=10 and g(n)=n )
For all values of n >=1, this relationship will be maintained
So, we can infer that f (n) = O (n)
Now, what if 3n + 5 < 10 *(n^2) (Here, C=10 and g(n)=n^2 ) ?
For all values of n >=1, this relationship will be maintained
So, we can also infer that f (n) = O (n^2)
Both are correct but generally, we tend to write the answer which is more close to the exact one. So, f (n) = O (n) is the closest
correct answer.
 Big Omega
The function f (n) = Omega (g (n)) if there exists positive constants C and n0 such that f(n) >= C*g(n) where n > n0
For example,
Let f(n) = 3n + 5
Then, we can write that –
3n + 5 >= 2n (Here, C=2 and g(n)=n ))
Hence we can write this notation as f (n) = Omega (n)
Similarly,
f (n) = Omega (log n), considering g(n)= log n
f (n) = Omega (sqrt(n)), considering g(n)= sqrt(n) are also correct.
 Big Theta
The function f (n) = Theta (g (n)) if there exists positive constants C1, C2 and n0 such that f (n) = C*g(n) where n > n0.
For example,
Let f (n) = 3n + 5
Then, we can write that –
1 <= 3n + 5 <= n^2 (for n > 0)
Here, although left bound and right bound are approximately equal i.e. Omega (1) and O (n^2), It is more precise to substitute
the exact value which is nothing but n. This called average bound. It is denoted as Theta (n).
f(n) = Theta(n)
Now, you will probably have a doubt that why to use Big O and Omega when we can always use Theta to get the exact
complexity rather than approximate ones.
Searching def:-
 Searching in data structure refers to the process of finding the required information from a collection of items stored as elements in the computer
memory.
 These sets of items are in different forms, such as an array, linked list, graph, or tree. Another way to define searching in the data structures is by
locating the desired element of specific characteristics in a collection of items.
 Searching in the data structure can be done by applying searching algorithms to check for or extract an element from any form of stored data
structure.
 These algorithms are classified according to the type of search operation they perform, such as:
Sequential search
The list or array of elements is traversed sequentially while checking every component of the set.
Example – Linear Search.
Interval Search
The interval search includes algorithms that are explicitly designed for searching in sorted data structures. In terms of efficiency, these algorithms
are far better than linear search algorithms.
Example- Logarithmic Search, Binary search.
 These methods are evaluated based on the time taken by an algorithm to search an element matching the search item in the
data collections and are given by,
The best possible time
The average time
The worst-case time
 The primary concerns are with worst-case times, which provide guaranteed predictions of the algorithm’s performance and
are also easier to calculate than average times.
 There are numerous searching algorithms in a data structure such as linear search, binary search, interpolation search, sublist
search, exponential search, jump search, Fibonacci search, the ubiquitous binary search, recursive function for substring search,
unbounded, binary search, and recursive program to search an element linearly in the given array.
Searching process-
 Searching is a process of finding a particular element among several given elements.
 The search is successful if the required element is found.
 Otherwise, the search is unsuccessful.
Searching Algorithms-
 Searching Algorithms are a family of algorithms used for the purpose of searching.
 The searching of an element in the given array may be carried out in the following two ways-
Linear Search
 Linear Search is the simplest searching algorithm.
 It traverses the array sequentially to locate the required element.
 It searches for an element by comparing it with each element of the array one by one.
 So, it is also called as Sequential Search.
 Linear Search Algorithm is applied when-
No information is given about the array.
The given array is unsorted, or the elements are unordered.
The list of data items is smaller.
 Linear Search Algorithm-
There is a linear array ‘a’ of size ‘n’.
Linear search algorithm is being used to search an element ‘item’ in this linear array.
If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1.
• Linear Search Efficiency-
Linear Search is less efficient when compared with other algorithms like Binary Search & Hash tables.
The other algorithms allow significantly faster searching.
 Linear_Search (a , n , item , loc)
Begin
for i = 0 to (n - 1) by 1 do
if (a[i] = item) then
set loc = i
Exit
endif
endfor
set loc = -1
End
 Time Complexity Analysis- Linear Search time complexity analysis is done below-
Best case- In the best possible case,
The element being searched may be found at the first position.
In this case, the search terminates in success with just one comparison.
Thus, in best case, linear search algorithm takes O(1) operations.
Worst Case- In the worst possible case,
The element being searched may be present at the last position or not present in the array at all.
In the former case, the search terminates in success with n comparisons.
In the later case, the search terminates in failure with n comparisons.
Thus, in worst case, linear search algorithm takes O(n) operations.
Time Complexity of Linear Search Algorithm is O(n).
Here, n is the number of elements in the linear array.
Linear Search Example-
 We are given the following linear array.
 Element 15 to be searched in it using Linear Search Algorithm.
 Linear Search algorithm compares element 15 with all the elements of the array one by one.
 It continues searching until either the element 15 is found or all the elements are searched.
 Linear Search Algorithm works in the following steps-
Step-1:
It compares element 15 with the 1st element 92.
Since 15 ≠ 92, so required element is not found.
So, it moves to the next element.
Step-2:
It compares element 15 with the 2nd element 87.
Since 15 ≠ 87, so required element is not found.
So, it moves to the next element.
Step-3:
It compares element 15 with the 3rd element 53.
Since 15 ≠ 53, so required element is not found.
So, it moves to the next element.
Step-4:
It compares element 15 with the 4th element 10.
Since 15 ≠ 10, so required element is not found.
So, it moves to the next element.
Step-5:
It compares element 15 with the 5th element 15.
Since 15 = 15, so required element is found.
Now, it stops the comparison and returns index 4 at which element 15 is present.
Binary Search
 Binary Search is one of the fastest searching algorithms.
 It is used for finding the location of an element in a linear array.
 It works on the principle of divide and conquer technique.
 Binary Search Algorithm can be applied only on Sorted arrays
.
 So, the elements must be arranged -
either in ascending order if the elements are numbers.
or in dictionary order if the elements are strings.
 To apply binary search on an unsorted array,
First, sort the array using some sorting technique.
Then, use binary search algorithm.
Binary Search Algorithm-
 There is a linear array ‘a’ of size ‘n’.
 Binary search algorithm is being used to search an element ‘item’ in this linear array.
 If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1.
 Variables beg and end keeps track of the index of the first and last element of the array or sub array in which the element is
being searched at that instant.
 Variable mid keeps track of the index of the middle element of that array or sub array in which the element is being searched
at that instant.
Binary Search Algorithm Advantages-
 It eliminates half of the list from further searching by using the result of each comparison.
 It indicates whether the element being searched is before or after the current position in the list.
 This information is used to narrow the search.
 For large lists of data, it works significantly better than linear search.
Binary Search Algorithm Disadvantages-
 It employs recursive approach which requires more stack space.
 Programming binary search algorithm is error prone and difficult.
 The interaction of binary search with memory hierarchy i.e. caching is poor. (because of its random access nature)
Begin
Set beg = 0
Set end = n-1
Set mid = (beg + end) / 2
while ( (beg <= end) and (a[mid] ≠ item) ) do
if (item < a[mid]) then
Set end = mid - 1
else
Set beg = mid + 1
endif
Set mid = (beg + end) / 2
endwhile
if (beg > end) then
Set loc = -1
else
Set loc = mid
endif
End
Time Complexity Analysis- Binary Search time complexity analysis is done as below-
 In each iteration or in each recursive call, the search gets reduced to half of the array.
 So, for n elements in the array, there are log2n iterations or recursive calls.
 Thus, we have Time Complexity of Binary Search Algorithm is O(log2n).
Here, n is the number of elements in the sorted linear array.
 This time complexity of binary search remains unchanged irrespective of the element position even if it is not present in the
array.
Explanation
 Binary Search Algorithm searches an element by comparing it with the middle most element of the array.
 Then, following three cases are possible-
Case-1
If the element being searched is found to be the middle most element, its index is returned.
Case-2
If the element being searched is found to be greater than the middle most element, then its search is further continued in the
right sub array of the middle most element.
Case-3
If the element being searched is found to be smaller than the middle most element, then its search is further continued in
the left sub array of the middle most element.
This iteration keeps on repeating on the sub arrays until the desired element is found or size of the sub array reduces to
zero.
Interpolation search
 Interpolation search is a searching algorithm that applies to a sorted & equally distributed array, and it is an Improved
variant of Binary Search .
 Like binary search, it uses the divide and conquers algorithm, but unlikely, it does not divide the array into two equal parts
to search the element.
 As compared to a linear search, binary search is more efficient, but the Interpolation search is more effective than any other
searching algorithm
 With Binary searching, if we want to locate the position of an element in the array, we require O(log n) time complexity,
but we have another searching algorithm that can search an element with O(log log n) time complexity.
 As binary search always checks for the middle index, rather than considering the element, the interpolation search,
consider the searched element and based on an algorithm check for those locations for which the value of the element
being searched.
 Time Complexity:-
Linear Search Binary Search Interpolation Search
O(n) O(log n) O(log log n)
Algorithm
The array must be sorted
Make a start =0 and end = n-1
Use a formula to locate the position, which brings us near to the searched element
Pos = start + (((end-start)/(arr[end]-arr[start])) * (target – arr[start]))
If arr[pos] == target return the pos
If target > arr[pos] start = pos+1
Else end = pos-1
Complexities in interpolation search are given below:
 When the middle (our approximation) is the desired key, Interpolation Search works best. As a result, the best case time
complexity is O(1).
 If the data set is sorted and distributed uniformly, the interpolation search’s average time complexity is O(log2(log2n)),
where n denotes the total of elements in an array.
 In the worst-case scenario, we’ll have to traverse the entire array, which will take O(n) time.
 An interpolation search is used when the location of the target element is known in the data collection. If we want to find
Rahul’s phone number in the phone book, instead of using a linear or binary search, you can directly probe to memory space
storage where names begin with ‘R’.

More Related Content

Similar to Searching.pptx

Theory of algorithms final
Theory of algorithms final Theory of algorithms final
Theory of algorithms final Dgech
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureRai University
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notationirdginfo
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureRai University
 
DATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTESDATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTESAniruddha Paul
 
DATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxDATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxShivamKrPathak
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoreambikavenkatesh2
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxskilljiolms
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsAbdullah Al-hazmy
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexityshowkat27
 
Data Structures Notes
Data Structures NotesData Structures Notes
Data Structures NotesRobinRohit2
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsNagendraK18
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxPJS KUMAR
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusNANDINI SHARMA
 
Data structures notes for college students btech.pptx
Data structures notes for college students btech.pptxData structures notes for college students btech.pptx
Data structures notes for college students btech.pptxKarthikVijay59
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxkassahungebrie
 

Similar to Searching.pptx (20)

Theory of algorithms final
Theory of algorithms final Theory of algorithms final
Theory of algorithms final
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
Sortsearch
SortsearchSortsearch
Sortsearch
 
DATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTESDATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTES
 
Complexity
ComplexityComplexity
Complexity
 
DATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxDATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptx
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
 
Data Structures Notes
Data Structures NotesData Structures Notes
Data Structures Notes
 
Big o
Big oBig o
Big o
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
my docoment
my docomentmy docoment
my docoment
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptx
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
Data structures notes for college students btech.pptx
Data structures notes for college students btech.pptxData structures notes for college students btech.pptx
Data structures notes for college students btech.pptx
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
 

Recently uploaded

Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
The SRE Report 2024 - Great Findings for the teams
The SRE Report 2024 - Great Findings for the teamsThe SRE Report 2024 - Great Findings for the teams
The SRE Report 2024 - Great Findings for the teamsDILIPKUMARMONDAL6
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 

Recently uploaded (20)

Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
The SRE Report 2024 - Great Findings for the teams
The SRE Report 2024 - Great Findings for the teamsThe SRE Report 2024 - Great Findings for the teams
The SRE Report 2024 - Great Findings for the teams
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 

Searching.pptx

  • 2. Dictionary as an ADT  A dictionary is an abstract data type that defines an unordered collection of data as a set of key-value pairs.  Each key, which must be unique, has an associated value.  Typically, the keys in a dictionary must be simple types (such as integers or strings) while the values can be of any type.  Standard dictionary operations include: retrieve a value associated with a particular key insert a new key-value pair into the collection remove a key-value pair from the collection update a value associated with a key  Using a key allows direct access to items stored within the collection. This provides much quicker access to unordered data than searching through the data set using a linear search and will also outperform a binary search. Key-value retrieval is a technique widely used in various high-performance systems, such as caches and databases.
  • 3. Defining a dictionary  Many languages provide a dictionary (sometimes called an associative array) as a built-in data type; in others we may find a library that includes an appropriate implementation that we can use.  Different languages enforce different type restrictions on keys and values in a dictionary.  Dictionaries are often implemented as hash tables.  Here we will consider the use of a built-in type and present pseudocode for main operation. DICTIONARY results = {"Detra" : 17, "Nova" : 84,"Charlie" : 22, "Hwa" : 75, "Roxan" : 92,"Elsa" : 29, "Barbara": Null}  Here a dictionary has been defined in pseudocode. The identifier (name) of the dictionary is results.  The key is the name of the student, and the value is the result of a recent assessment.  This is a simplistic example to demonstrate the main principles.  The use of a student's first name as a key is unlikely to be a sensible choice as we would probably have more than one student with the same name (and key values must be unique).  Note that a dictionary is unordered. No attempt should be made to keep the data in any kind of order, as it is arranged to optimize the speed of retrieving a value by key rather than by position.
  • 4. Algorithmic Notation  Algorithm notations are used to compare efficiency between different algorithms, the basics are:  o(n) , O(n) , Θ(n) , Ω(n) , ω(n) ,:- faster , faster or equal , equal to , slower or equal to , slower than, o(n) - faster , O(n) - faster or equal , Θ(n) - equal to, Ω(n) - slower or equal to , ω(n) - slower than  We want to pay extra attention to O(n)O(n) since it is either faster or equal, which takes the worst case scenario into account, which is equal efficiency.  Now that we know this, we might have a program: Load: → O(n2)O(n2) Execute part 1: → O(nlogn)O(nlogn) Execute part 2: → O(n)O(n) Save: → O(1)O(1) Then you can add up all of these: O(n2) + O(nlogn) + O(n) + O(1)+ O(n2) + O(nlogn) + O(n) + O(1) to calculate the speed of the program. In this case, n2n2is the worst case scenario.
  • 5.  There are basically 3 types of asymptotic notations- Big O Omega (also called Big Omega) Theta (also called Big Theta)  The above 3 asymptotic notations are used for measuring the time complexity of an algorithm.  The time complexity of a program statement is nothing but the number of times it gets executed. For example, 1. The following code snippet will execute only once if it’s get compiled on a machine. Hence, its time complexity will be O (1). 2. The following for loop has executed n times where n = 10. Here, whatever value we substitute for n, it will run for that number of times. Hence, its time complexity will be O (n). int n=10,count=0; for(i=0;i<n;i++) count=count+1; printf(“%d”,count);
  • 6. 3. The following code has executed n*n times which is clear from its logic. This code snippet will have a complexity of O (n^2). 4. The following code snippet will give an output corresponding to the input in such a manner that-- Output = (input * (input + 1))/2 This is nothing but n(n+1) / 2 where n is the input More precisely, n (n+1) / 2 = (n^2 + n) /2 We can approximately take this value as n^2. Hence, here also time complexity will be O(n^2) int n=10,count=0; for(i=0;i<n;i++) { for(j=0;j<i+1;j++) count=count+1; } printf(“%d”,count); int n=5,count=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) count=count+1; } printf(“%d”,count);
  • 7.  Big O is nothing but a notation to specify the upper bound of an algorithm’s time complexity.  Omega is nothing but a notation to specify the lower bound of an algorithm’s time complexity.  Theta is nothing but a notation to specify the exact bound of an algorithm’s time complexity. Any algorithm’s time complexity will be any one of the below list or at least multiple of any of these elements-  1  log n  sqrt(N)  n  n log n  n^2  n^3  2^n  3^n  n ^ n
  • 8.  Big O The function f (n) = O (g (n)) if there exists positive constants C and n0 such that f(n) <= C*g(n) where n > n0 For example, Let f(n) = 3n + 5 Then, we can write that – 3n + 5 < 10n (Here, C=10 and g(n)=n ) For all values of n >=1, this relationship will be maintained So, we can infer that f (n) = O (n) Now, what if 3n + 5 < 10 *(n^2) (Here, C=10 and g(n)=n^2 ) ? For all values of n >=1, this relationship will be maintained So, we can also infer that f (n) = O (n^2) Both are correct but generally, we tend to write the answer which is more close to the exact one. So, f (n) = O (n) is the closest correct answer.
  • 9.  Big Omega The function f (n) = Omega (g (n)) if there exists positive constants C and n0 such that f(n) >= C*g(n) where n > n0 For example, Let f(n) = 3n + 5 Then, we can write that – 3n + 5 >= 2n (Here, C=2 and g(n)=n )) Hence we can write this notation as f (n) = Omega (n) Similarly, f (n) = Omega (log n), considering g(n)= log n f (n) = Omega (sqrt(n)), considering g(n)= sqrt(n) are also correct.
  • 10.  Big Theta The function f (n) = Theta (g (n)) if there exists positive constants C1, C2 and n0 such that f (n) = C*g(n) where n > n0. For example, Let f (n) = 3n + 5 Then, we can write that – 1 <= 3n + 5 <= n^2 (for n > 0) Here, although left bound and right bound are approximately equal i.e. Omega (1) and O (n^2), It is more precise to substitute the exact value which is nothing but n. This called average bound. It is denoted as Theta (n). f(n) = Theta(n) Now, you will probably have a doubt that why to use Big O and Omega when we can always use Theta to get the exact complexity rather than approximate ones.
  • 11. Searching def:-  Searching in data structure refers to the process of finding the required information from a collection of items stored as elements in the computer memory.  These sets of items are in different forms, such as an array, linked list, graph, or tree. Another way to define searching in the data structures is by locating the desired element of specific characteristics in a collection of items.  Searching in the data structure can be done by applying searching algorithms to check for or extract an element from any form of stored data structure.  These algorithms are classified according to the type of search operation they perform, such as: Sequential search The list or array of elements is traversed sequentially while checking every component of the set. Example – Linear Search. Interval Search The interval search includes algorithms that are explicitly designed for searching in sorted data structures. In terms of efficiency, these algorithms are far better than linear search algorithms. Example- Logarithmic Search, Binary search.
  • 12.  These methods are evaluated based on the time taken by an algorithm to search an element matching the search item in the data collections and are given by, The best possible time The average time The worst-case time  The primary concerns are with worst-case times, which provide guaranteed predictions of the algorithm’s performance and are also easier to calculate than average times.  There are numerous searching algorithms in a data structure such as linear search, binary search, interpolation search, sublist search, exponential search, jump search, Fibonacci search, the ubiquitous binary search, recursive function for substring search, unbounded, binary search, and recursive program to search an element linearly in the given array.
  • 13. Searching process-  Searching is a process of finding a particular element among several given elements.  The search is successful if the required element is found.  Otherwise, the search is unsuccessful. Searching Algorithms-  Searching Algorithms are a family of algorithms used for the purpose of searching.  The searching of an element in the given array may be carried out in the following two ways-
  • 14. Linear Search  Linear Search is the simplest searching algorithm.  It traverses the array sequentially to locate the required element.  It searches for an element by comparing it with each element of the array one by one.  So, it is also called as Sequential Search.  Linear Search Algorithm is applied when- No information is given about the array. The given array is unsorted, or the elements are unordered. The list of data items is smaller.  Linear Search Algorithm- There is a linear array ‘a’ of size ‘n’. Linear search algorithm is being used to search an element ‘item’ in this linear array. If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1. • Linear Search Efficiency- Linear Search is less efficient when compared with other algorithms like Binary Search & Hash tables. The other algorithms allow significantly faster searching.
  • 15.  Linear_Search (a , n , item , loc) Begin for i = 0 to (n - 1) by 1 do if (a[i] = item) then set loc = i Exit endif endfor set loc = -1 End  Time Complexity Analysis- Linear Search time complexity analysis is done below- Best case- In the best possible case, The element being searched may be found at the first position. In this case, the search terminates in success with just one comparison. Thus, in best case, linear search algorithm takes O(1) operations. Worst Case- In the worst possible case, The element being searched may be present at the last position or not present in the array at all. In the former case, the search terminates in success with n comparisons. In the later case, the search terminates in failure with n comparisons. Thus, in worst case, linear search algorithm takes O(n) operations. Time Complexity of Linear Search Algorithm is O(n). Here, n is the number of elements in the linear array.
  • 16. Linear Search Example-  We are given the following linear array.  Element 15 to be searched in it using Linear Search Algorithm.  Linear Search algorithm compares element 15 with all the elements of the array one by one.  It continues searching until either the element 15 is found or all the elements are searched.  Linear Search Algorithm works in the following steps- Step-1: It compares element 15 with the 1st element 92. Since 15 ≠ 92, so required element is not found. So, it moves to the next element. Step-2: It compares element 15 with the 2nd element 87. Since 15 ≠ 87, so required element is not found. So, it moves to the next element.
  • 17. Step-3: It compares element 15 with the 3rd element 53. Since 15 ≠ 53, so required element is not found. So, it moves to the next element. Step-4: It compares element 15 with the 4th element 10. Since 15 ≠ 10, so required element is not found. So, it moves to the next element. Step-5: It compares element 15 with the 5th element 15. Since 15 = 15, so required element is found. Now, it stops the comparison and returns index 4 at which element 15 is present.
  • 18. Binary Search  Binary Search is one of the fastest searching algorithms.  It is used for finding the location of an element in a linear array.  It works on the principle of divide and conquer technique.  Binary Search Algorithm can be applied only on Sorted arrays .  So, the elements must be arranged - either in ascending order if the elements are numbers. or in dictionary order if the elements are strings.  To apply binary search on an unsorted array, First, sort the array using some sorting technique. Then, use binary search algorithm.
  • 19. Binary Search Algorithm-  There is a linear array ‘a’ of size ‘n’.  Binary search algorithm is being used to search an element ‘item’ in this linear array.  If search ends in success, it sets loc to the index of the element otherwise it sets loc to -1.  Variables beg and end keeps track of the index of the first and last element of the array or sub array in which the element is being searched at that instant.  Variable mid keeps track of the index of the middle element of that array or sub array in which the element is being searched at that instant. Binary Search Algorithm Advantages-  It eliminates half of the list from further searching by using the result of each comparison.  It indicates whether the element being searched is before or after the current position in the list.  This information is used to narrow the search.  For large lists of data, it works significantly better than linear search. Binary Search Algorithm Disadvantages-  It employs recursive approach which requires more stack space.  Programming binary search algorithm is error prone and difficult.  The interaction of binary search with memory hierarchy i.e. caching is poor. (because of its random access nature)
  • 20. Begin Set beg = 0 Set end = n-1 Set mid = (beg + end) / 2 while ( (beg <= end) and (a[mid] ≠ item) ) do if (item < a[mid]) then Set end = mid - 1 else Set beg = mid + 1 endif Set mid = (beg + end) / 2 endwhile if (beg > end) then Set loc = -1 else Set loc = mid endif End Time Complexity Analysis- Binary Search time complexity analysis is done as below-  In each iteration or in each recursive call, the search gets reduced to half of the array.  So, for n elements in the array, there are log2n iterations or recursive calls.  Thus, we have Time Complexity of Binary Search Algorithm is O(log2n). Here, n is the number of elements in the sorted linear array.  This time complexity of binary search remains unchanged irrespective of the element position even if it is not present in the array.
  • 21. Explanation  Binary Search Algorithm searches an element by comparing it with the middle most element of the array.  Then, following three cases are possible- Case-1 If the element being searched is found to be the middle most element, its index is returned. Case-2 If the element being searched is found to be greater than the middle most element, then its search is further continued in the right sub array of the middle most element. Case-3 If the element being searched is found to be smaller than the middle most element, then its search is further continued in the left sub array of the middle most element. This iteration keeps on repeating on the sub arrays until the desired element is found or size of the sub array reduces to zero.
  • 22. Interpolation search  Interpolation search is a searching algorithm that applies to a sorted & equally distributed array, and it is an Improved variant of Binary Search .  Like binary search, it uses the divide and conquers algorithm, but unlikely, it does not divide the array into two equal parts to search the element.  As compared to a linear search, binary search is more efficient, but the Interpolation search is more effective than any other searching algorithm  With Binary searching, if we want to locate the position of an element in the array, we require O(log n) time complexity, but we have another searching algorithm that can search an element with O(log log n) time complexity.  As binary search always checks for the middle index, rather than considering the element, the interpolation search, consider the searched element and based on an algorithm check for those locations for which the value of the element being searched.  Time Complexity:- Linear Search Binary Search Interpolation Search O(n) O(log n) O(log log n)
  • 23. Algorithm The array must be sorted Make a start =0 and end = n-1 Use a formula to locate the position, which brings us near to the searched element Pos = start + (((end-start)/(arr[end]-arr[start])) * (target – arr[start])) If arr[pos] == target return the pos If target > arr[pos] start = pos+1 Else end = pos-1 Complexities in interpolation search are given below:  When the middle (our approximation) is the desired key, Interpolation Search works best. As a result, the best case time complexity is O(1).  If the data set is sorted and distributed uniformly, the interpolation search’s average time complexity is O(log2(log2n)), where n denotes the total of elements in an array.  In the worst-case scenario, we’ll have to traverse the entire array, which will take O(n) time.  An interpolation search is used when the location of the target element is known in the data collection. If we want to find Rahul’s phone number in the phone book, instead of using a linear or binary search, you can directly probe to memory space storage where names begin with ‘R’.