SlideShare uma empresa Scribd logo
1 de 35
SEARCHING
Unit 8
Introduction
• Search can be viewed abstractly as a process to determine if an
element with a particular value is a member of a particular set.
• Searching refers to finding out whether a particular element is present
in the list or not.
• Search algorithms are algorithms that find one element of a set by
some key containing other information related to the key.
• If the list is an unordered list, then we use linear or sequential search,
whereas if the list is an ordered list, then we use binary search.
Ashim Lamichhane 2
Searching Terminologies
• Internal key
• Key is contained within the record at a specific offset from the start of the
record
• External Key
• There is a separate table of keys that includes pointers to the records
• Primary Key
• The key used to search is unique for every record in the table
• Secondary Key
• Same key may be associated with more than one record in the table
Ashim Lamichhane 3
Searching Terminologies
• Search algorithm
• Accepts an argument and tries to find a record in a given table whose key it matches
• Successful search
• Algorithm finds at least one record
• Unsuccessful search
• Algorithm doesn’t find any records
• Retrieval
• A successful search is often called retrieval
• Internal Searching
• Records to be searched are sorted entirely within computer main memory
• External Searching
• For large records we need external storage devices and we search on such devices.
Ashim Lamichhane 4
Searching Algorithms
• Basically there are two categories of searching algorithms
Sequential Search or Linear Search
Binary Search
Ashim Lamichhane 5
Linear or Sequential Search
• This the simplest method for searching, in this method the element to
be found is searched sequentially in the list.
• This method can be used on a sorted or an unsorted list.
• In case of a sorted list searching starts from 0th element and continues
until the element is found or the element whose value is greater than
the value being searched is reached
• Searching in case of unsorted list starts from the 0th element and
continues until the element is found or the end of the list is reached.
Ashim Lamichhane 6
Linear or Sequential Search
• The list given above is the list of elements in an unsorted array.
• The array contains 10 elements, suppose the element to be searched
is 46 then 46 is compared with all the elements starting from the 0th
element and searching process ends where 46 is found or the list
ends.
• The performance of the linear search can be measured by counting
the comparison done to find out an element.
• The number of comparisons is 0(n)
Ashim Lamichhane 7
Binary Search
• Binary search technique is very fast and efficient. It requires the list of the elements to be in sorted
order.
• The logic behind this technique is given below:
• First find the middle element of the array
• compare the middle element with an item.
• There are three cases:
• If it is a desired element then search is successful
• If it is less than desired item then search only the first half of the array.
• If it is greater than the desired element, search in the second half of the array.
• Repeat the same process until element is found or exhausts in the search area.
• In this algorithm every time we are reducing the search area.
• We can apply binary search technique recursively or iteratively
Ashim Lamichhane 8
Recursive Binary Search
• As the list is divided into two halves, searching begins
• Now we check if the searched item is greater or less than the center
element.
• If the element is smaller than the center element then the searching is
done in the first half, otherwise it is done in the second half
• The process is repeated till the element is found or the division of half
parts gives one element.
Ashim Lamichhane 9
Recursive Binary Search
• Let a[n] be a sorted array of size n and x be an item to be searched. Let
low=0 and high=n-1 be lower and upper bound of an array
1. If (low>high)
return 0;
2. mid=(low+high)/2;
3. if (x==a[mid])
return mid;
4. if (x<a[mid])
Search for x in a[low] to a[mid-1];
5. else
Search for x in a[mid+1] to a[high];
Ashim Lamichhane 10
Ashim Lamichhane 11
• We need to search 46, we can assign first=0 and last= 9 since first and
last are the initial and final position/index of an array
• The binary search when applied to this array works as follows:
1. Find center of the list i.e. (first+last)/2 =4 i.e. mid=array[4]=10
2. If higher than the mid element we search only on the upper half of an array
else search lower half. In this case since 46>10, we search the upper half
i.e. array[mid+1] to array [n-1]
3. The process is repeated till 46 is found or no further subdivision of array is
possible
0 1 2 9 10 11 15 20 46 72
Ashim Lamichhane 12
Iterative Binary Search Algorithm
• The recursive algorithm is inappropriate in practical situations in which
efficiency is a prime consideration.
• The non-recursive version of binary search algorithm is given below.
• Let a[n] be a sorted array with size n and key is an item being searched
for.
Ashim Lamichhane 13
Algorithm
low=0; hi=n-1;
while(low<=hi){
mid=(low+hi)/2;
if(key==a[mid])
return mid;
if (key<a[mid])
hi=mid-1;
else
low=mid+1;
}
return 0;
Ashim Lamichhane 14
Ashim Lamichhane 15
Comparison of sequential and binary search algorithm
Sequential Search Binary Search
Time complexity: 0(n) Time complexity: 0(logn)
Only eliminates one element from the search per
comparison
Eliminates half of the array elements for each
comparison
As n number of elements gets very large, sequential
search has to do lot more work
As n number or elements gets very large, binary
search has to do little work than sequential search
Has to search each element i,i+1,i+2,i+3…….n Has to search n/2,n/4,n/8….1 elements
Doesn’t require element to be in sorted order Requires elements to be in sorted order
Easy to program but takes too much time Easy to program and execution time faster
Ashim Lamichhane 16
Application of Search Algorithms
• Search Engines
• Online enquiry
• Text Pattern matching
• Spell Checker
• Ant algorithm
Ashim Lamichhane 17
Hashing Intro
• Now the only problem at hands is to speed up searching.
• Consider a problem of searching an array for a given value.
• If array is not sorted, need to search each and every elements
• If array is sorted, binary search whose worse-case runtime complexity 0(logn)
• We could search even faster if we know in advance the index at which that value is
located in the array.
• We could use some magic function where our search would be reduced to a
constant runtime 0(1). Such magic function is called hash function.
• A hash function is a function which when given a key, generates an address in the
table
Ashim Lamichhane 18
Hashing
• The process of mapping large amounts of data into a smaller table is
called hashing.
• Hashing is relatively easy to program as compared to tree however it is
based on arrays, hence difficult to expand
• It performs insertions, deletions and finds in constant average time.
Ashim Lamichhane 19
Hash Table
• A hash table is a data structure where we store a key value after applying the hash
function, it is arranged in the form of an array that is addressed via a hash
function.
• The hash table is divided into number of buckets and each bucket is in turn
capable of storing a number of record.
• Thus we can say that a bucket has a number of slots and each slot is capable of
holding one record
• The time required to locate any element in the hash table is 0(1).
• Now the question may arise how do we map number of keys to a particular
location in the hash table. i.e. h(k). It is computed using hash function
Ashim Lamichhane 20
0
1 21
2
3
4
5
6 56
7
8
9
Ashim Lamichhane 21
Hash Table
Array Items: 21, 56, 72 ,39, 48,96,13….
H(x) =x mod 10
H(21) = 21 mod 10 = 1
H(56) = 56 mod 10 = 6
H(72)=….
H(39)=….
Hash Function
• A function that transforms a key into a table index is called a hash function
• If h is a hash function and k is a key) is called the hash of key and is the index in which a
record with the key k should be placed.
• The basic idea in hashing is the transformation of a key into the corresponding location in
the hash table. This is done by hash function.
• It is usually denoted by H
H: K -> M
where, H=hash function
K= set of keys
M=set of memory address
Ashim Lamichhane 22
Hash Functions
• Sometimes, such function H may not yield distinct values, It is possible that two
different keys K1 and K2 will yield the same hash address.
• This situation is called Hash Collision
• To choose the hash function H:K->M there are two things to consider.
• First the function H should be very easy and quick to compute.
• Second, H should distribute the keys to the number of location of hash table with less no of
collisions
• Some hash functions:
• Division reminder method
• Mid square method
• Folding method
Ashim Lamichhane 23
Division Reminder Method
• In this method, key k is divided by a number m larger than the number
n or keys in k and the reminder of this division is taken as index into
the hash table i.e.
h(k)=k mod m
• The number m is usually chosen to be a prime number or a number
without small divisors, since this frequently minimizes the number of
collisions
Ashim Lamichhane 24
Division Reminder Method
• Consider a hash table with 7 slots i.e, m=7, then hash function
h(k)= k mod n will map the key 8 into slot 1 since
h(8)=8 mod 7 =1 is mapped to slot 1,
h(13)=13 mod 7 = 6 is mapped to slot 6 and so on.
Ashim Lamichhane 25
Mid Square Method
• In this method the key is first squared. Therefore the hash function is defined by h(k) =p,
where p is obtained by deleting digits from both sides of k2.
• To implement this the same position of k2 must be used for all the keys.
• Ex: consider a hash table with 50 slots i.e m=50 and key values k =1632,1739,3123.
• Solution:
k = 1632
k2 = 2663424
h(k) = 34
Ashim Lamichhane 26
Folding Method
• In this method the key, k is partitioned into a number of parts k1,k2….kr
where each part, except possibly the last, has the same number of
digits as the required address.
• Then the parts are added together, ignoring the last carry i.e.
H(k)= k1+k2+…..+kr
Ashim Lamichhane 27
Hash Collision
• hash collision is the process in which more than one key is mapped to the same
memory allocation in the table.
• For example, if we are using the division reminder hashing:
h(k) = k%7
Then key =8 and key =15 both mapped to the same location of the table
h(k) = 8 % 7 = 1
h(k) = 15 % 7 =1
• Two most common methods to resolve the hash collision are as follows:
• Hash collision resolution by separate chaining
• Hash collision Resolution by Open Addressing
Ashim Lamichhane 28
Hash collision resolution by separate chaining
• In this method all the elements where keys hash to the same hash-
table slot are put in one linked list
• Therefore the slot in the hash table contains a pointer to the head of
the linked list
Ashim Lamichhane 29
Hash collision Resolution by Open Addressing
• In open addressing the keys to be hashed is to put in the separate location of the
hash table.
• Each location contains some key or the some other character to indicate that the
particular location is free
• Some popular methods
• Linear probing
• Quadratic probing
• Double Hashing
• Rehashing
Ashim Lamichhane 30
Linear probing
Ashim Lamichhane 31
• A hash-table in which a collision is resolved by putting the item in the
next empty place within the occupied array space
• It starts with a location where the collision occurred and does a
sequential search through a hash table for the desired empty location
• Hence the method searches in straight line, and it is therefore called
linear probing.
• Example: insert keys {89,18,49,58,69} with the hash function h(x) = x
mod 10 using linear probing
Ashim Lamichhane 32
Quadratic probing
• Quadratic probing is a collision resolution method that eliminates the primary collision
problem.
• When collision occur then the quadratic probing works as follows:
(Hash value +12) % tablesize
• If there is again collision then there exist rehashing
(Hash value +22) % tablesize
• If there is again collision then there exist rehashing
(Hash value +32) % tablesize
• Same goes again, in ith collision
h(x) =(hash value + i2) % tablesize
Ashim Lamichhane 33
Ashim Lamichhane 34
Assignments
• Slides at myblog
www.ashimlamichhane.com
• Assignments at github
https://github.com/ashim888/dataStructureAndAlgorithm/tree/dev/As
signments/assignment_9
Ashim Lamichhane 35

Mais conteúdo relacionado

Mais procurados

linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear searchmontazur420
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structureMeherul1234
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary searchNisha Soms
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search AlgorithmFazalRehman79
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsMohamed Loey
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation AhmedAlbutty
 

Mais procurados (20)

linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
Hash table
Hash tableHash table
Hash table
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Sorting
SortingSorting
Sorting
 
Binary search
Binary searchBinary search
Binary search
 
stack & queue
stack & queuestack & queue
stack & queue
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Hashing
HashingHashing
Hashing
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Linked list
Linked listLinked list
Linked list
 

Semelhante a Searching

searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptxDr.Shweta
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithmstechnologygyan
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsAnushdika Jeganathan
 
Hashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesHashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesssuserec8a711
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing conceptsLJ Projects
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing conceptsLJ Projects
 
Binary search algorithm.pptx
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptxbhuvansaachi18
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxMohammed472103
 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptxHODElex
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhithRj Juhith
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxsgrishma559
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing TablesChinmaya M. N
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHISowmya Jyothi
 

Semelhante a Searching (20)

searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Hashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniquesHashing techniques, Hashing function,Collision detection techniques
Hashing techniques, Hashing function,Collision detection techniques
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Binary search algorithm.pptx
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptx
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptx
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Data analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptxData analysis and algorithms - UNIT 2.pptx
Data analysis and algorithms - UNIT 2.pptx
 
cs702 ppt.ppt
cs702 ppt.pptcs702 ppt.ppt
cs702 ppt.ppt
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing Tables
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
 
Hashing
HashingHashing
Hashing
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 

Mais de Ashim Lamichhane

Mais de Ashim Lamichhane (18)

Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Linked List
Linked ListLinked List
Linked List
 
Queues
QueuesQueues
Queues
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
 
Unit 11. Graphics
Unit 11. GraphicsUnit 11. Graphics
Unit 11. Graphics
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
 

Último

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Último (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

Searching

  • 2. Introduction • Search can be viewed abstractly as a process to determine if an element with a particular value is a member of a particular set. • Searching refers to finding out whether a particular element is present in the list or not. • Search algorithms are algorithms that find one element of a set by some key containing other information related to the key. • If the list is an unordered list, then we use linear or sequential search, whereas if the list is an ordered list, then we use binary search. Ashim Lamichhane 2
  • 3. Searching Terminologies • Internal key • Key is contained within the record at a specific offset from the start of the record • External Key • There is a separate table of keys that includes pointers to the records • Primary Key • The key used to search is unique for every record in the table • Secondary Key • Same key may be associated with more than one record in the table Ashim Lamichhane 3
  • 4. Searching Terminologies • Search algorithm • Accepts an argument and tries to find a record in a given table whose key it matches • Successful search • Algorithm finds at least one record • Unsuccessful search • Algorithm doesn’t find any records • Retrieval • A successful search is often called retrieval • Internal Searching • Records to be searched are sorted entirely within computer main memory • External Searching • For large records we need external storage devices and we search on such devices. Ashim Lamichhane 4
  • 5. Searching Algorithms • Basically there are two categories of searching algorithms Sequential Search or Linear Search Binary Search Ashim Lamichhane 5
  • 6. Linear or Sequential Search • This the simplest method for searching, in this method the element to be found is searched sequentially in the list. • This method can be used on a sorted or an unsorted list. • In case of a sorted list searching starts from 0th element and continues until the element is found or the element whose value is greater than the value being searched is reached • Searching in case of unsorted list starts from the 0th element and continues until the element is found or the end of the list is reached. Ashim Lamichhane 6
  • 7. Linear or Sequential Search • The list given above is the list of elements in an unsorted array. • The array contains 10 elements, suppose the element to be searched is 46 then 46 is compared with all the elements starting from the 0th element and searching process ends where 46 is found or the list ends. • The performance of the linear search can be measured by counting the comparison done to find out an element. • The number of comparisons is 0(n) Ashim Lamichhane 7
  • 8. Binary Search • Binary search technique is very fast and efficient. It requires the list of the elements to be in sorted order. • The logic behind this technique is given below: • First find the middle element of the array • compare the middle element with an item. • There are three cases: • If it is a desired element then search is successful • If it is less than desired item then search only the first half of the array. • If it is greater than the desired element, search in the second half of the array. • Repeat the same process until element is found or exhausts in the search area. • In this algorithm every time we are reducing the search area. • We can apply binary search technique recursively or iteratively Ashim Lamichhane 8
  • 9. Recursive Binary Search • As the list is divided into two halves, searching begins • Now we check if the searched item is greater or less than the center element. • If the element is smaller than the center element then the searching is done in the first half, otherwise it is done in the second half • The process is repeated till the element is found or the division of half parts gives one element. Ashim Lamichhane 9
  • 10. Recursive Binary Search • Let a[n] be a sorted array of size n and x be an item to be searched. Let low=0 and high=n-1 be lower and upper bound of an array 1. If (low>high) return 0; 2. mid=(low+high)/2; 3. if (x==a[mid]) return mid; 4. if (x<a[mid]) Search for x in a[low] to a[mid-1]; 5. else Search for x in a[mid+1] to a[high]; Ashim Lamichhane 10
  • 11. Ashim Lamichhane 11 • We need to search 46, we can assign first=0 and last= 9 since first and last are the initial and final position/index of an array • The binary search when applied to this array works as follows: 1. Find center of the list i.e. (first+last)/2 =4 i.e. mid=array[4]=10 2. If higher than the mid element we search only on the upper half of an array else search lower half. In this case since 46>10, we search the upper half i.e. array[mid+1] to array [n-1] 3. The process is repeated till 46 is found or no further subdivision of array is possible 0 1 2 9 10 11 15 20 46 72
  • 13. Iterative Binary Search Algorithm • The recursive algorithm is inappropriate in practical situations in which efficiency is a prime consideration. • The non-recursive version of binary search algorithm is given below. • Let a[n] be a sorted array with size n and key is an item being searched for. Ashim Lamichhane 13
  • 14. Algorithm low=0; hi=n-1; while(low<=hi){ mid=(low+hi)/2; if(key==a[mid]) return mid; if (key<a[mid]) hi=mid-1; else low=mid+1; } return 0; Ashim Lamichhane 14
  • 16. Comparison of sequential and binary search algorithm Sequential Search Binary Search Time complexity: 0(n) Time complexity: 0(logn) Only eliminates one element from the search per comparison Eliminates half of the array elements for each comparison As n number of elements gets very large, sequential search has to do lot more work As n number or elements gets very large, binary search has to do little work than sequential search Has to search each element i,i+1,i+2,i+3…….n Has to search n/2,n/4,n/8….1 elements Doesn’t require element to be in sorted order Requires elements to be in sorted order Easy to program but takes too much time Easy to program and execution time faster Ashim Lamichhane 16
  • 17. Application of Search Algorithms • Search Engines • Online enquiry • Text Pattern matching • Spell Checker • Ant algorithm Ashim Lamichhane 17
  • 18. Hashing Intro • Now the only problem at hands is to speed up searching. • Consider a problem of searching an array for a given value. • If array is not sorted, need to search each and every elements • If array is sorted, binary search whose worse-case runtime complexity 0(logn) • We could search even faster if we know in advance the index at which that value is located in the array. • We could use some magic function where our search would be reduced to a constant runtime 0(1). Such magic function is called hash function. • A hash function is a function which when given a key, generates an address in the table Ashim Lamichhane 18
  • 19. Hashing • The process of mapping large amounts of data into a smaller table is called hashing. • Hashing is relatively easy to program as compared to tree however it is based on arrays, hence difficult to expand • It performs insertions, deletions and finds in constant average time. Ashim Lamichhane 19
  • 20. Hash Table • A hash table is a data structure where we store a key value after applying the hash function, it is arranged in the form of an array that is addressed via a hash function. • The hash table is divided into number of buckets and each bucket is in turn capable of storing a number of record. • Thus we can say that a bucket has a number of slots and each slot is capable of holding one record • The time required to locate any element in the hash table is 0(1). • Now the question may arise how do we map number of keys to a particular location in the hash table. i.e. h(k). It is computed using hash function Ashim Lamichhane 20
  • 21. 0 1 21 2 3 4 5 6 56 7 8 9 Ashim Lamichhane 21 Hash Table Array Items: 21, 56, 72 ,39, 48,96,13…. H(x) =x mod 10 H(21) = 21 mod 10 = 1 H(56) = 56 mod 10 = 6 H(72)=…. H(39)=….
  • 22. Hash Function • A function that transforms a key into a table index is called a hash function • If h is a hash function and k is a key) is called the hash of key and is the index in which a record with the key k should be placed. • The basic idea in hashing is the transformation of a key into the corresponding location in the hash table. This is done by hash function. • It is usually denoted by H H: K -> M where, H=hash function K= set of keys M=set of memory address Ashim Lamichhane 22
  • 23. Hash Functions • Sometimes, such function H may not yield distinct values, It is possible that two different keys K1 and K2 will yield the same hash address. • This situation is called Hash Collision • To choose the hash function H:K->M there are two things to consider. • First the function H should be very easy and quick to compute. • Second, H should distribute the keys to the number of location of hash table with less no of collisions • Some hash functions: • Division reminder method • Mid square method • Folding method Ashim Lamichhane 23
  • 24. Division Reminder Method • In this method, key k is divided by a number m larger than the number n or keys in k and the reminder of this division is taken as index into the hash table i.e. h(k)=k mod m • The number m is usually chosen to be a prime number or a number without small divisors, since this frequently minimizes the number of collisions Ashim Lamichhane 24
  • 25. Division Reminder Method • Consider a hash table with 7 slots i.e, m=7, then hash function h(k)= k mod n will map the key 8 into slot 1 since h(8)=8 mod 7 =1 is mapped to slot 1, h(13)=13 mod 7 = 6 is mapped to slot 6 and so on. Ashim Lamichhane 25
  • 26. Mid Square Method • In this method the key is first squared. Therefore the hash function is defined by h(k) =p, where p is obtained by deleting digits from both sides of k2. • To implement this the same position of k2 must be used for all the keys. • Ex: consider a hash table with 50 slots i.e m=50 and key values k =1632,1739,3123. • Solution: k = 1632 k2 = 2663424 h(k) = 34 Ashim Lamichhane 26
  • 27. Folding Method • In this method the key, k is partitioned into a number of parts k1,k2….kr where each part, except possibly the last, has the same number of digits as the required address. • Then the parts are added together, ignoring the last carry i.e. H(k)= k1+k2+…..+kr Ashim Lamichhane 27
  • 28. Hash Collision • hash collision is the process in which more than one key is mapped to the same memory allocation in the table. • For example, if we are using the division reminder hashing: h(k) = k%7 Then key =8 and key =15 both mapped to the same location of the table h(k) = 8 % 7 = 1 h(k) = 15 % 7 =1 • Two most common methods to resolve the hash collision are as follows: • Hash collision resolution by separate chaining • Hash collision Resolution by Open Addressing Ashim Lamichhane 28
  • 29. Hash collision resolution by separate chaining • In this method all the elements where keys hash to the same hash- table slot are put in one linked list • Therefore the slot in the hash table contains a pointer to the head of the linked list Ashim Lamichhane 29
  • 30. Hash collision Resolution by Open Addressing • In open addressing the keys to be hashed is to put in the separate location of the hash table. • Each location contains some key or the some other character to indicate that the particular location is free • Some popular methods • Linear probing • Quadratic probing • Double Hashing • Rehashing Ashim Lamichhane 30
  • 31. Linear probing Ashim Lamichhane 31 • A hash-table in which a collision is resolved by putting the item in the next empty place within the occupied array space • It starts with a location where the collision occurred and does a sequential search through a hash table for the desired empty location • Hence the method searches in straight line, and it is therefore called linear probing.
  • 32. • Example: insert keys {89,18,49,58,69} with the hash function h(x) = x mod 10 using linear probing Ashim Lamichhane 32
  • 33. Quadratic probing • Quadratic probing is a collision resolution method that eliminates the primary collision problem. • When collision occur then the quadratic probing works as follows: (Hash value +12) % tablesize • If there is again collision then there exist rehashing (Hash value +22) % tablesize • If there is again collision then there exist rehashing (Hash value +32) % tablesize • Same goes again, in ith collision h(x) =(hash value + i2) % tablesize Ashim Lamichhane 33
  • 35. Assignments • Slides at myblog www.ashimlamichhane.com • Assignments at github https://github.com/ashim888/dataStructureAndAlgorithm/tree/dev/As signments/assignment_9 Ashim Lamichhane 35