SlideShare a Scribd company logo
Chapter 11
Searching
Dr. Muhammad Hanif Durad
Department of Computer and Information Sciences
Pakistan Institute Engineering and Applied Sciences
hanif@pieas.edu.pk
Some slides have bee adapted with thanks from some other lectures
available on Internet. It made my life easier, as life is always
miserable at PIEAS (Sir Muhammad Yusaf Kakakhil )
Lecture Outline
 Searching Concept
 Linear Search
 Binary Search
 Interpolation Search
Searching Concepts(1/3)
 The problem of locating an element in a list
(ordered or not) occurs in many contexts.
 For instance, a program that checks the spelling
of words searches for them in a dictionary,
which is just an ordered list of words.
 Problems of this kind are called searching
problems.
D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
Searching Concepts(2/3)
 There are many searching algorithms.
 The natural searching method is linear search
(or sequential search, or exhaustive search),
which is very simple but takes a long time
when applying with large lists.
D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
Searching Concepts(3/3)
 A binary search repeatedly subdivides the list
to locate an item and for larger lists it is much
faster than linear search.
 Like a binary search, an interpolation search
repeatedly subdivides the list to locate an item.
 Interpolation search is much faster than binary
search because it makes a reasonable guess
about where the target item should lie
D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
Searching Problem
INPUT
• sequence of numbers (database)
• a single number (query)
a1, a2, a3,….,an; v j
OUTPUT
• an index of the found
number or NIL
2 5 4 10 7; 5 2
2 5 4 10 7; 9 NIL
D:Data StructuresHanif_SearchSearching ad1.ppt, P-24
Linear Search (1/8)
 This is a very simple algorithm.
 It uses a loop to sequentially step through
an array, starting with the first element.
 It compares each element with the value
being searched for and stops when that
value is found or the end of the array is
reached.
D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
Linear Search-Pseudo Code (2/8)
LINEAR_SEARCH(A,v)
1. for i←1 to n
2. do if A[i]=v
3. then return I
4. return NIL
DSAL-4,P-13
Linear Search (3/8)
 Array A contains
 Searching for the the value 11, linear search examines
17, 23, 5, and 11 -> Found (i = 4)
 Searching for the the value 7, linear search examines
17, 23, 5, 11, 2, 29, and 3 -> Not Found (i =NIL)
17 23 5 11 2 29 3
D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
Linear Search (4/8)
 The advantage is its simplicity.
 It is easy to understand
 Easy to implement
 Does not require the array to be in order
 The disadvantage is its inefficiency
 If there are 20,000 items in the array and what
you are looking for is in the 19,999th element,
you need to search through the entire list.
Linear Search (5/8)
 Whenever the number of entries doubles, so
does the running time, roughly.
 If a machine does 1 million comparisons per
second, it takes about 30 minutes for
4 billion comparisons.
Linear Search (6/8)
0
5
10
15
20
25
n=10 n=20 n=30 n=40
Time
Analysis of Linear Search (7/8)
 Complexity: O(n)
 On average, n/2 comparisons is needed
 Best case: the first element searched is the
value we want
 Worst case: the last element searched is the
value we want
D:Data StructuresHanif_SearchSearching 07i_searching.ppt
Average Case Analysis of Linear
Search (8/8)
Suppose that there are n elements in the array. The following expression
gives the average number of comparisons:
It is known that
Therefore, the following expression gives the average number of comparisons
made by the sequential search in the successful case:
D:Data StructuresHanif_SearchSearching Linear + Binary search.ppt
Binary Search (1/10)
Can We Search More Efficiently?
 Yes, provided the list is in some kind of order,
for example alphabetical order with respect to
the names.
 If this is the case, we use a “divide and conquer”
strategy to find an item quickly.
 This strategy is what one would use in a
“number guessing game”, for example.
D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
Binary Search (2/10)
I’m Thinking of A Number…
 … between 1 and 1000. Guess it!
 Is it 500? Nope, too low.
 Is it 750? Nope, too high.
 Is it 625? … etc…
This strategy guarantees a correct guess in
no more than ten guesses!
Binary Search (3/10)
Apply This Strategy to Searching
 The resulting algorithm is called the “Binary
Search” algorithm.
 We check the middle key in our list.
 If it is beyond what we are looking for (too
high), we look only at the top half of the list.
 If it’s not far enough in (too low), we look at
the bottom half.
 Then iterate!
Binary Search Steps (4/10)
1. Divide a sorted array into three sections.
 middle element
 elements on one side of the middle element
 elements on the other side of the middle
element
2. If the middle element is the correct value,
done. Otherwise, go to step 1, using only the
half of the array that may contain the correct
value.
Binary Search Steps (5/10)
3. Continue steps 1 and 2 until either the
value is found or there are no more
elements to examine.
Binary Search-Pseudo Code-1
(6/10)
RECURSIVE_BINARY _SEARCH (A, v, low, high )
1. if low > high
2. then return NIL
3. mid ←
4. If v = A[mid]
5. then return mid
6. If v > A[mid]
7. then return RECURSIVE_BINARY _SEARCH (A, v, mid+1, high )
8. else return RECURSIVE_BINARY _SEARCH (A, v, low, mid-1)
DSAL-4,P-18
( )/2low high  
Binary Search-Pseudo Code-2
(7/10)
ITERATIVE_BINARY _SEARCH (A, v, low, high )
1. While low high
2. do mid ←
3. If v = A[mid]
4. then return mid
5. else if v > A[mid]
6. then low ← mid+1
7. else high← mid-1
8. return NIL
DSAL-4,P-18
( )/2low high  

Binary Search (8/10)
 The worst case number of comparisons grows by
only 1 comparison every time list size is doubled.
 Only 32 comparisons would be needed on a list
of 4 billion using Binary Search. (Sequential
Search would need 4 billion comparisons and
would take 30 minutes!)
D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
 Considering the worst-case for binary search:
 We don’t find the item until we have divided the array as far as it will
divide
 We first look at the middle of n items, then we look at the
middle of n/2 items, then n/22 items, and so on…
 We will divide until n/2k = 1, k is the number of times we have
divided the set (when we have divided all we can, the above
equation will be true)
 n/2k = 1 when n = 2k, so to find out how many times we
divided the set, we solve for k
k = log2 n
 Thus, the algorithm takes O(log n) , the worst-case (we ingore
logarithmic base)
Binary Search (9/10)
D:Data StructuresHanif_SearchSearching cset3150_algo_search.ppt
Binary Search (10/10)
 Benefit
 Much more efficient than linear search.
 For array of N elements, performs at most log2N
comparisons.
 Disadvantage
 Requires that array elements be sorted.
Interpolation Search
 Binary search is a great improvement over
linear search because it eliminates large
portion of the list without actually examing
all the eliminated values.
 If we know that the values are fairly evenly
distributed, we can use interpolation to
eliminate even more values at each step.
D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
Interpolation Search
 Interpolation is the process of using known
values to guess where an unknown value lies.
 We use the indexes of known values in the
list to guess what index the target value
should have.
 Interpolation search selects the dividing point
by interpolation using the following code
m = l + (x – A[l])*(r-l)/(A[r]-A[l])
Interpolation Search
D:Data StructuresHanif_SearchSearching 07i_searching.ppt
Interpolation Search
 Requirement: the list of data is sorted
Interpolation Search
 Compare x to A[m]
 If x = A[m]: Found.
 If x<A[m]: set r = m-1
 If x > A[m]: set l = m + 1
 If searching is still not finish, continue searching
with new l and r.
 Stop searching when Found or x<A[l] or x>A[r].
D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
Interpolation Search
Example: Find the key x = 32 in the list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 4 7 9 9 12 13 17 19 21 24 32 36 44 45 54 55 63 66 70
1: l=1, r=20 -> m=1+(32-1)*(20-1)/(70-1) = 10
a[10]=21<32=x -> l=11
2: l=11, r=20 -> m=11+(30-24)*(20-11)/(70-24) = 12
a[12]=32=x -> Found at m = 12
Interpolation Search
Example: Find the key x = 30 in the list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 4 7 9 9 12 13 17 19 21 24 32 36 44 45 54 55 63 66 70
1: l=1, r=20 -> m=1+(30-1)*(20-1)/(70-1) = 9
a[9]=19<30=x -> l=10
2: l=10, r=20 -> m=10+(30-21)*(20-10)/(70-21) = 12
a[12]=32>30=x -> r = 11
3: l=10, r=11 -> m=10+(30-24)*(11-10)/(24-21) = 12
m=12>11=r: Not Found
Interpolation Search-Pseudo
Code (1/2)
Private Sub Interpolation(a[]: Int, x: Int, n: Int,
Found: Boolean)
l = 1: r = n
Do While (r > l)
m = l + ((x – a[l]) / (a[r] – a[l])) * (r - l)
‘Verify and Decise What to do next
Loop
End Sub
Interpolation Search-Pseudo
Code (2/2)
‘Verify and Decide what to do next
If (a[m] = x) Or (m < l) Or (m > r) Then
Found = iif(a[m] = x, True, False)
Exit Do
ElseIf (a[m] < x) Then
l = m + 1
ElseIf (a[m] > x) Then
r = m – 1
End If
Interpolation Search
 Binary search is very fast (O(logn)), but
interpolation search is much faster (O(loglogn)).
 For n = 2^32 (four billion items)
 Binary search took 32 steps of verification
 Interpolation search took only 5 steps of
verification.
Interpolation Search
 Interpolation search performance time is
nearly constant for a large range of n.
 Interpolation is still more usefull if the data
had been stored on a hard disk or other
relatively slow device.

More Related Content

What's hot

Data Structure
Data StructureData Structure
Data Structure
Karthikeyan A K
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
Kuber Chandra
 
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queuesFallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
SnehilKeshari
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
Abbott
 
Data structures using C
Data structures using CData structures using C
Data structures using C
Pdr Patnaik
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
Usha Mahalingam
 
Data Structures (BE)
Data Structures (BE)Data Structures (BE)
Data Structures (BE)
PRABHAHARAN429
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Hoang Nguyen
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
BG Java EE Course
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
iqbalphy1
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
Data structures
Data structuresData structures
Data structures
Saurabh Mishra
 
Data structures
Data structuresData structures
Data structures
Sneha Chopra
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
sumitbardhan
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
Nguync91368
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
Prof. Dr. K. Adisesha
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
Nguync91368
 

What's hot (20)

Data Structure
Data StructureData Structure
Data Structure
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queuesFallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Data Structures (BE)
Data Structures (BE)Data Structures (BE)
Data Structures (BE)
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Data structures
Data structuresData structures
Data structures
 
Data structures
Data structuresData structures
Data structures
 
358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8358 33 powerpoint-slides_8-linked-lists_chapter-8
358 33 powerpoint-slides_8-linked-lists_chapter-8
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 

Similar to Chapter 11 ds

Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
arpitaeron555
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
Revathiparamanathan
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
9781111530532 ppt ch14
9781111530532 ppt ch149781111530532 ppt ch14
9781111530532 ppt ch14
Terry Yoast
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESPYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
vanithasivdc
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
Terry Yoast
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
mbadhi barnabas
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
21BD1A058RSahithi
 
ODD EVEN BASED BINARY SEARCH
ODD EVEN BASED BINARY SEARCHODD EVEN BASED BINARY SEARCH
ODD EVEN BASED BINARY SEARCH
IAEME Publication
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
Durga Devi
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in ds
chauhankapil
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
sajinis3
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 

Similar to Chapter 11 ds (20)

Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
 
UNIT V.docx
UNIT V.docxUNIT V.docx
UNIT V.docx
 
UNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdfUNIT IV -Data Structures.pdf
UNIT IV -Data Structures.pdf
 
9781111530532 ppt ch14
9781111530532 ppt ch149781111530532 ppt ch14
9781111530532 ppt ch14
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESPYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
ODD EVEN BASED BINARY SEARCH
ODD EVEN BASED BINARY SEARCHODD EVEN BASED BINARY SEARCH
ODD EVEN BASED BINARY SEARCH
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 
Binary search in ds
Binary search in dsBinary search in ds
Binary search in ds
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 

More from Hanif Durad

Chapter 26 aoa
Chapter 26 aoaChapter 26 aoa
Chapter 26 aoa
Hanif Durad
 
Chapter 25 aoa
Chapter 25 aoaChapter 25 aoa
Chapter 25 aoa
Hanif Durad
 
Chapter 24 aoa
Chapter 24 aoaChapter 24 aoa
Chapter 24 aoa
Hanif Durad
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
Hanif Durad
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
Hanif Durad
 
Chapter 10 ds
Chapter 10 dsChapter 10 ds
Chapter 10 ds
Hanif Durad
 
Chapter 9 ds
Chapter 9 dsChapter 9 ds
Chapter 9 ds
Hanif Durad
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
Hanif Durad
 
Chapter 3 ds
Chapter 3 dsChapter 3 ds
Chapter 3 ds
Hanif Durad
 
Chapter 2 ds
Chapter 2 dsChapter 2 ds
Chapter 2 ds
Hanif Durad
 
Chapter 5 pc
Chapter 5 pcChapter 5 pc
Chapter 5 pc
Hanif Durad
 
Chapter 4 pc
Chapter 4 pcChapter 4 pc
Chapter 4 pc
Hanif Durad
 
Chapter 3 pc
Chapter 3 pcChapter 3 pc
Chapter 3 pc
Hanif Durad
 
Chapter 2 pc
Chapter 2 pcChapter 2 pc
Chapter 2 pc
Hanif Durad
 
Chapter 1 pc
Chapter 1 pcChapter 1 pc
Chapter 1 pc
Hanif Durad
 
Chapter 6 pc
Chapter 6 pcChapter 6 pc
Chapter 6 pc
Hanif Durad
 
Collective Communications in MPI
 Collective Communications in MPI Collective Communications in MPI
Collective Communications in MPI
Hanif Durad
 
Point-to-Point Communicationsin MPI
Point-to-Point Communicationsin MPIPoint-to-Point Communicationsin MPI
Point-to-Point Communicationsin MPI
Hanif Durad
 
Introduction to MPI
Introduction to MPI Introduction to MPI
Introduction to MPI
Hanif Durad
 

More from Hanif Durad (19)

Chapter 26 aoa
Chapter 26 aoaChapter 26 aoa
Chapter 26 aoa
 
Chapter 25 aoa
Chapter 25 aoaChapter 25 aoa
Chapter 25 aoa
 
Chapter 24 aoa
Chapter 24 aoaChapter 24 aoa
Chapter 24 aoa
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Chapter 10 ds
Chapter 10 dsChapter 10 ds
Chapter 10 ds
 
Chapter 9 ds
Chapter 9 dsChapter 9 ds
Chapter 9 ds
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
Chapter 3 ds
Chapter 3 dsChapter 3 ds
Chapter 3 ds
 
Chapter 2 ds
Chapter 2 dsChapter 2 ds
Chapter 2 ds
 
Chapter 5 pc
Chapter 5 pcChapter 5 pc
Chapter 5 pc
 
Chapter 4 pc
Chapter 4 pcChapter 4 pc
Chapter 4 pc
 
Chapter 3 pc
Chapter 3 pcChapter 3 pc
Chapter 3 pc
 
Chapter 2 pc
Chapter 2 pcChapter 2 pc
Chapter 2 pc
 
Chapter 1 pc
Chapter 1 pcChapter 1 pc
Chapter 1 pc
 
Chapter 6 pc
Chapter 6 pcChapter 6 pc
Chapter 6 pc
 
Collective Communications in MPI
 Collective Communications in MPI Collective Communications in MPI
Collective Communications in MPI
 
Point-to-Point Communicationsin MPI
Point-to-Point Communicationsin MPIPoint-to-Point Communicationsin MPI
Point-to-Point Communicationsin MPI
 
Introduction to MPI
Introduction to MPI Introduction to MPI
Introduction to MPI
 

Recently uploaded

Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 

Recently uploaded (20)

Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 

Chapter 11 ds

  • 1. Chapter 11 Searching Dr. Muhammad Hanif Durad Department of Computer and Information Sciences Pakistan Institute Engineering and Applied Sciences hanif@pieas.edu.pk Some slides have bee adapted with thanks from some other lectures available on Internet. It made my life easier, as life is always miserable at PIEAS (Sir Muhammad Yusaf Kakakhil )
  • 2. Lecture Outline  Searching Concept  Linear Search  Binary Search  Interpolation Search
  • 3. Searching Concepts(1/3)  The problem of locating an element in a list (ordered or not) occurs in many contexts.  For instance, a program that checks the spelling of words searches for them in a dictionary, which is just an ordered list of words.  Problems of this kind are called searching problems. D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
  • 4. Searching Concepts(2/3)  There are many searching algorithms.  The natural searching method is linear search (or sequential search, or exhaustive search), which is very simple but takes a long time when applying with large lists. D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
  • 5. Searching Concepts(3/3)  A binary search repeatedly subdivides the list to locate an item and for larger lists it is much faster than linear search.  Like a binary search, an interpolation search repeatedly subdivides the list to locate an item.  Interpolation search is much faster than binary search because it makes a reasonable guess about where the target item should lie D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
  • 6. Searching Problem INPUT • sequence of numbers (database) • a single number (query) a1, a2, a3,….,an; v j OUTPUT • an index of the found number or NIL 2 5 4 10 7; 5 2 2 5 4 10 7; 9 NIL D:Data StructuresHanif_SearchSearching ad1.ppt, P-24
  • 7. Linear Search (1/8)  This is a very simple algorithm.  It uses a loop to sequentially step through an array, starting with the first element.  It compares each element with the value being searched for and stops when that value is found or the end of the array is reached. D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
  • 8. Linear Search-Pseudo Code (2/8) LINEAR_SEARCH(A,v) 1. for i←1 to n 2. do if A[i]=v 3. then return I 4. return NIL DSAL-4,P-13
  • 9. Linear Search (3/8)  Array A contains  Searching for the the value 11, linear search examines 17, 23, 5, and 11 -> Found (i = 4)  Searching for the the value 7, linear search examines 17, 23, 5, 11, 2, 29, and 3 -> Not Found (i =NIL) 17 23 5 11 2 29 3 D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
  • 10. Linear Search (4/8)  The advantage is its simplicity.  It is easy to understand  Easy to implement  Does not require the array to be in order  The disadvantage is its inefficiency  If there are 20,000 items in the array and what you are looking for is in the 19,999th element, you need to search through the entire list.
  • 11. Linear Search (5/8)  Whenever the number of entries doubles, so does the running time, roughly.  If a machine does 1 million comparisons per second, it takes about 30 minutes for 4 billion comparisons.
  • 13. Analysis of Linear Search (7/8)  Complexity: O(n)  On average, n/2 comparisons is needed  Best case: the first element searched is the value we want  Worst case: the last element searched is the value we want D:Data StructuresHanif_SearchSearching 07i_searching.ppt
  • 14. Average Case Analysis of Linear Search (8/8) Suppose that there are n elements in the array. The following expression gives the average number of comparisons: It is known that Therefore, the following expression gives the average number of comparisons made by the sequential search in the successful case: D:Data StructuresHanif_SearchSearching Linear + Binary search.ppt
  • 15. Binary Search (1/10) Can We Search More Efficiently?  Yes, provided the list is in some kind of order, for example alphabetical order with respect to the names.  If this is the case, we use a “divide and conquer” strategy to find an item quickly.  This strategy is what one would use in a “number guessing game”, for example. D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
  • 16. Binary Search (2/10) I’m Thinking of A Number…  … between 1 and 1000. Guess it!  Is it 500? Nope, too low.  Is it 750? Nope, too high.  Is it 625? … etc… This strategy guarantees a correct guess in no more than ten guesses!
  • 17. Binary Search (3/10) Apply This Strategy to Searching  The resulting algorithm is called the “Binary Search” algorithm.  We check the middle key in our list.  If it is beyond what we are looking for (too high), we look only at the top half of the list.  If it’s not far enough in (too low), we look at the bottom half.  Then iterate!
  • 18. Binary Search Steps (4/10) 1. Divide a sorted array into three sections.  middle element  elements on one side of the middle element  elements on the other side of the middle element 2. If the middle element is the correct value, done. Otherwise, go to step 1, using only the half of the array that may contain the correct value.
  • 19. Binary Search Steps (5/10) 3. Continue steps 1 and 2 until either the value is found or there are no more elements to examine.
  • 20. Binary Search-Pseudo Code-1 (6/10) RECURSIVE_BINARY _SEARCH (A, v, low, high ) 1. if low > high 2. then return NIL 3. mid ← 4. If v = A[mid] 5. then return mid 6. If v > A[mid] 7. then return RECURSIVE_BINARY _SEARCH (A, v, mid+1, high ) 8. else return RECURSIVE_BINARY _SEARCH (A, v, low, mid-1) DSAL-4,P-18 ( )/2low high  
  • 21. Binary Search-Pseudo Code-2 (7/10) ITERATIVE_BINARY _SEARCH (A, v, low, high ) 1. While low high 2. do mid ← 3. If v = A[mid] 4. then return mid 5. else if v > A[mid] 6. then low ← mid+1 7. else high← mid-1 8. return NIL DSAL-4,P-18 ( )/2low high   
  • 22. Binary Search (8/10)  The worst case number of comparisons grows by only 1 comparison every time list size is doubled.  Only 32 comparisons would be needed on a list of 4 billion using Binary Search. (Sequential Search would need 4 billion comparisons and would take 30 minutes!) D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
  • 23.  Considering the worst-case for binary search:  We don’t find the item until we have divided the array as far as it will divide  We first look at the middle of n items, then we look at the middle of n/2 items, then n/22 items, and so on…  We will divide until n/2k = 1, k is the number of times we have divided the set (when we have divided all we can, the above equation will be true)  n/2k = 1 when n = 2k, so to find out how many times we divided the set, we solve for k k = log2 n  Thus, the algorithm takes O(log n) , the worst-case (we ingore logarithmic base) Binary Search (9/10) D:Data StructuresHanif_SearchSearching cset3150_algo_search.ppt
  • 24. Binary Search (10/10)  Benefit  Much more efficient than linear search.  For array of N elements, performs at most log2N comparisons.  Disadvantage  Requires that array elements be sorted.
  • 25. Interpolation Search  Binary search is a great improvement over linear search because it eliminates large portion of the list without actually examing all the eliminated values.  If we know that the values are fairly evenly distributed, we can use interpolation to eliminate even more values at each step. D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
  • 26. Interpolation Search  Interpolation is the process of using known values to guess where an unknown value lies.  We use the indexes of known values in the list to guess what index the target value should have.  Interpolation search selects the dividing point by interpolation using the following code m = l + (x – A[l])*(r-l)/(A[r]-A[l])
  • 28. Interpolation Search  Requirement: the list of data is sorted
  • 29. Interpolation Search  Compare x to A[m]  If x = A[m]: Found.  If x<A[m]: set r = m-1  If x > A[m]: set l = m + 1  If searching is still not finish, continue searching with new l and r.  Stop searching when Found or x<A[l] or x>A[r]. D:Data StructuresHanif_SearchSearchingLecture 08 - Searching Algorithms.ppt
  • 30. Interpolation Search Example: Find the key x = 32 in the list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 4 7 9 9 12 13 17 19 21 24 32 36 44 45 54 55 63 66 70 1: l=1, r=20 -> m=1+(32-1)*(20-1)/(70-1) = 10 a[10]=21<32=x -> l=11 2: l=11, r=20 -> m=11+(30-24)*(20-11)/(70-24) = 12 a[12]=32=x -> Found at m = 12
  • 31. Interpolation Search Example: Find the key x = 30 in the list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 4 7 9 9 12 13 17 19 21 24 32 36 44 45 54 55 63 66 70 1: l=1, r=20 -> m=1+(30-1)*(20-1)/(70-1) = 9 a[9]=19<30=x -> l=10 2: l=10, r=20 -> m=10+(30-21)*(20-10)/(70-21) = 12 a[12]=32>30=x -> r = 11 3: l=10, r=11 -> m=10+(30-24)*(11-10)/(24-21) = 12 m=12>11=r: Not Found
  • 32. Interpolation Search-Pseudo Code (1/2) Private Sub Interpolation(a[]: Int, x: Int, n: Int, Found: Boolean) l = 1: r = n Do While (r > l) m = l + ((x – a[l]) / (a[r] – a[l])) * (r - l) ‘Verify and Decise What to do next Loop End Sub
  • 33. Interpolation Search-Pseudo Code (2/2) ‘Verify and Decide what to do next If (a[m] = x) Or (m < l) Or (m > r) Then Found = iif(a[m] = x, True, False) Exit Do ElseIf (a[m] < x) Then l = m + 1 ElseIf (a[m] > x) Then r = m – 1 End If
  • 34. Interpolation Search  Binary search is very fast (O(logn)), but interpolation search is much faster (O(loglogn)).  For n = 2^32 (four billion items)  Binary search took 32 steps of verification  Interpolation search took only 5 steps of verification.
  • 35. Interpolation Search  Interpolation search performance time is nearly constant for a large range of n.  Interpolation is still more usefull if the data had been stored on a hard disk or other relatively slow device.