Lecture_Oct26.pptx

Searching Algorithm
• The searching algorithms are used to search or find one or more than
one element from a dataset. These type of algorithms are used to find
elements from a specific data structures.
• Searching may be sequential or not. If the data in the dataset are
random, then we need to use sequential searching. Otherwise we can
use other different techniques to reduce the complexity.
• Search algorithm is any algorithm which solves the search problem,
namely, to retrieve information stored within some data structure, or
calculated in the search space of a problem domain, either with discrete
or continuous values.
Searching Algorithms are designed to check for an element or retrieve
an element from any data structure where it is stored. Based on the
type of search operation, these algorithms are generally classified into
two categories:
• Sequential Search: In this, the list or array is traversed sequentially
and every element is checked. For example: Linear Search.
• Interval Search: These algorithms are specifically designed for
searching in sorted data-structures. These type of searching
algorithms are much more efficient than Linear Search as they
repeatedly target the center of the search structure and divide the
search space in half. For Example: Binary Search.
•
Linear Search
• a linear search or sequential search is a method for finding an
element within a list. It sequentially checks each element of the list
until a match is found or the whole list has been searched.[1]
• A linear search runs in at worst linear time and makes at
most n comparisons, where n is the length of the list. If each element
is equally likely to be searched, then linear search has an average case
of n+1/2 comparisons, but the average case can be affected if the
search probabilities for each element vary. Linear search is rarely
practical because other search algorithms and schemes, such as
the binary search algorithm and hash tables, allow significantly faster
searching for all but short lists.[2]
Time – Space Complexity
Worst complexity: O(n)
Average complexity: O(n)
Space complexity: O(1)
Worst-case space complexity: O(1) iterative
Average performance: O(n/2)
• A linear search sequentially checks each element of the list until it
finds an element that matches the target value. If the algorithm
reaches the end of the list, the search terminates unsuccessfully.[1]
Basic algorithm
• Given a list L of n elements with values or records L0 .... Ln−1, and
target value T, the following subroutine uses linear search to find the
index of the target T in L.[3]
• Set i to 0.
• If Li = T, the search terminates successfully; return i.
• Increase i by 1.
• If i < n, go to step 2. Otherwise, the search terminates unsuccessfully.
With a sentinel
• The basic algorithm above makes two comparisons per iteration: one
to check if Li equals T, and the other to check if i still points to a valid
index of the list. By adding an extra record Ln to the list (a sentinel
value) that equals the target, the second comparison can be
eliminated until the end of the search, making the algorithm faster.
The search will reach the sentinel if the target is not contained within
the list.[4]
• Set i to 0.
• If Li = T, go to step 4.
• Increase i by 1 and go to step 2.
• If i < n, the search terminates successfully; return i. Else, the search
terminates unsuccessfully.
In an ordered table
• If the list is ordered such that L0 ≤ L1 ... ≤ Ln−1, the search can establish
the absence of the target more quickly by concluding the search
once Li exceeds the target. This variation requires a sentinel that is
greater than the target.[5]
• Set i to 0.
• If Li ≥ T, go to step 4.
• Increase i by 1 and go to step 2.
• If Li = T, the search terminates successfully; return i. Else, the search
terminates unsuccessfully.
Lecture_Oct26.pptx
Lecture_Oct26.pptx
Homework:
Use Linear search to Search for: 1001 and 904
2, 13, 45, 67, 98, 101, 456, 501, 602, 899, 904, 1000
Binary Search
Binary Search: Search a sorted array by repeatedly dividing the search
interval in half. Begin with an interval covering the whole array. If the value of
the search key is less than the item in the middle of the interval, narrow the
interval to the lower half. Otherwise narrow it to the upper half. Repeatedly
check until the value is found or the interval is empty.
Binary search is a fast search algorithm with run-time complexity of Ο(log n).
This search algorithm works on the principle of divide and conquer. For this
algorithm to work properly, the data collection should be in the sorted form.
Binary search looks for a particular item by comparing the middle most item
of the collection. If a match occurs, then the index of item is returned. If the
middle item is greater than the item, then the item is searched in the sub-
array to the left of the middle item. Otherwise, the item is searched for in the
sub-array to the right of the middle item. This process continues on the sub-
array as well until the size of the subarray reduces to zero.
Binary search, also known as half-interval search, logarithmic search, or
binary chop, is a search algorithm that finds the position of a target value
within a sorted array. Binary search compares the target value to the
middle element of the array.
Time- Space Complexity
• Worst complexity: O(log n)
• Average complexity: O(log n)
• Best complexity: O(1)
• Space complexity: O(1)
• Data structure: Array
How Binary Search Works?
For a binary search to work, it is mandatory for the target array to be sorted.
We shall learn the process of binary search with a pictorial example. The
following is our sorted array and let us assume that we need to search the
location of value 31 using binary search.
First, we shall determine half of the array by using this formula −
mid = low + ((high - low) / 2 )
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
Now we compare the value stored at location 4, with the value being
searched, i.e. 31. We find that the value at location 4 is 27, which is not
a match. As the value is greater than 27 and we have a sorted array, so
we also know that the target value must be in the upper portion of the
array.
We change our low to mid + 1 and find the new mid value
again.
low = mid + 1 low = 4 + 1
mid = low + ((high - low) / 2) mid = 5 + ((9 – 5)/2)
mid = 5+2 = 7
Our new mid is 7 now. We compare the value stored at location 7
with our target value 31.
The value stored at location 7 is not a match, rather it is more than what we
are looking for. So, the value must be in the lower part from this location.
Hence, we calculate the mid again. This time it is 5.
low = mid + 1 low = 4 + 1
mid = low + ((high - low) / 2) mid = 5 + ((6 – 5)/2)
mid = 5+ 0.5 = 5.5 or 5
We compare the value stored at location 5 with our target value. We find that it is a match.
Lecture_Oct26.pptx
Lecture_Oct26.pptx
Lecture_Oct26.pptx
Lecture_Oct26.pptx
Lecture_Oct26.pptx
Lecture_Oct26.pptx
Jump Search
• Jump Search Algorithm is a relatively new algorithm for searching an
element in a sorted array.
• The fundamental idea behind this searching technique is to search
fewer number of elements compared to linear search
algorithm (which scans every element in the array to check if it
matches with the element being searched or not). This can be done
by skipping some fixed number of array elements or jumping ahead
by fixed number of steps in every iteration.
Jump Search is a searching algorithm for sorted arrays. The basic idea is
to check fewer elements (than linear search) by jumping ahead by fixed
steps or skipping some elements in place of searching all elements.
• For example, suppose we have an array arr[] of size n and block (to be
jumped) size m. Then we search at the indexes arr[0], arr[m],
arr[2m]…..arr[km] and so on. Once we find the interval (arr[km] < x <
arr[(k+1)m]), we perform a linear search operation from the index km
to find the element x.
Let’s consider the following array: (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
144, 233, 377, 610). Length of the array is 16. Jump search will find the
value of 55 with the following steps assuming that the block size to be
jumped is 4.
STEP 1: Jump from index 0 to index 4;
STEP 2: Jump from index 4 to index 8;
STEP 3: Jump from index 8 to index 12;
STEP 4: Since the element at index 12 is greater than 55 we will jump
back a step to come to index 8.
STEP 5: Perform linear search from index 8 to get the element 55.
What is the optimal block size to be skipped?
In the worst case, we have to do n/m jumps and if the last checked
value is greater than the element to be searched for, we perform m-1
comparisons more for linear search. Therefore the total number of
comparisons in the worst case will be ((n/m) + m-1). The value of the
function ((n/m) + m-1) will be minimum when m = √n. Therefore, the
best step size is m = √n.
How to choose the block or Jump size?
If the jump size is 1, then it will be a linear search. Hence we usually
take the jump size of SQRT(n). By doing so, in the worst case we do n/k
jumps.
Now let us understand the algorithm with help of example:
1, 1, 2, 3, 4, 10, 15, 20, 75, 80, 92, 95, 100
Search Key = 80
N = 13
Jump size = SQRT(13) = 3
Lecture_Oct26.pptx
Homework:
Given: - 1, 0, 5, 9, 23, 45, 55, 67, 78, 88, 99, 101, 105, 206
Perform Jump Search to search for: 206
Interpolation search
• Interpolation search is an improvement over binary search.
• Binary Search always checks the value at middle index. But,
interpolation search may check at different locations based on the
value of element being searched.
• For interpolation search to work efficiently the array elements/data
should be sorted and uniformly distributed.
• Steps:
• A - Array of elements, e - element to be searched, pos - current position
• Make start = 0 & end = n-1
• calculate position ( pos ) to start searching at using formula:
• If A[pos] == e , element found at index pos.
• Otherwise if e > A[pos] we make start = pos + 1
• Else if e < A[pos] we make end = pos -1
• Do stpes 3, 4, 5, 6 While : start <= end && e >= A[start] && e =< A[end]
• start <= end - that is until we have elements in the sub-array.
• e >= A[start] - element we are looking for is greater than or equal to the starting element of
sub-array we are looking in.
• e =< A[end] - element we are looking for is less than or equal to the last element of sub-
array we are looking in.
• Example if we are looking for element 4 in the given array.
Given: - 1, 0, 5, 9, 23, 45, 55, 67, 78, 88, 99, 101, 105, 206
Perform Interpolation Search to search for: 99
1 de 35

Recomendados

advanced searching and sorting.pdf por
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
117 vistas19 diapositivas
Searching techniques por
Searching techniquesSearching techniques
Searching techniquesArchana Burujwale
177 vistas30 diapositivas
Binary search Algorithm por
Binary search AlgorithmBinary search Algorithm
Binary search AlgorithmFazalRehman79
195 vistas19 diapositivas
Analysis of Algorithm - Binary Search.pptx por
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxMaulana Abul Kalam Azad University of Technology
167 vistas20 diapositivas
unit II_2_i.pptx por
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptxHODElex
4 vistas12 diapositivas
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI por
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
319 vistas40 diapositivas

Más contenido relacionado

Similar a Lecture_Oct26.pptx

Binary search algorithm.pptx por
Binary search  algorithm.pptxBinary search  algorithm.pptx
Binary search algorithm.pptxbhuvansaachi18
3 vistas12 diapositivas
Algorithm 8th lecture linear & binary search(2).pptx por
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAftabali702240
18 vistas22 diapositivas
Data operatons & searching and sorting algorithms por
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsAnushdika Jeganathan
111 vistas24 diapositivas
Chapter 11 - Sorting and Searching por
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
31.2K vistas58 diapositivas
Binary Search por
Binary SearchBinary Search
Binary Searchkunj desai
26.5K vistas13 diapositivas
DS - Unit 2 FINAL (2).pptx por
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxprakashvs7
18 vistas87 diapositivas

Similar a Lecture_Oct26.pptx(20)

Algorithm 8th lecture linear & binary search(2).pptx por Aftabali702240
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali70224018 vistas
Chapter 11 - Sorting and Searching por Eduardo Bergavera
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera31.2K vistas
Binary Search por kunj desai
Binary SearchBinary Search
Binary Search
kunj desai26.5K vistas
DS - Unit 2 FINAL (2).pptx por prakashvs7
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs718 vistas
Data Structures_ Sorting & Searching por ThenmozhiK5
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK566 vistas
Rahat &amp; juhith por Rj Juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
Rj Juhith556 vistas
Searching techniques in Data Structure And Algorithm por 03446940736
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
0344694073610K vistas
Algorithm & data structures lec4&5 por Abdul Khan
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
Abdul Khan3.9K vistas
Searching Sorting por guest2cb109
Searching SortingSearching Sorting
Searching Sorting
guest2cb1091.3K vistas
Binary Search - Design & Analysis of Algorithms por Drishti Bhalla
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla14.6K vistas

Último

Five Things You SHOULD Know About Postman por
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About PostmanPostman
33 vistas43 diapositivas
SAP Automation Using Bar Code and FIORI.pdf por
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdfVirendra Rai, PMP
23 vistas38 diapositivas
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... por
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...James Anderson
85 vistas32 diapositivas
Uni Systems for Power Platform.pptx por
Uni Systems for Power Platform.pptxUni Systems for Power Platform.pptx
Uni Systems for Power Platform.pptxUni Systems S.M.S.A.
56 vistas21 diapositivas
STPI OctaNE CoE Brochure.pdf por
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdfmadhurjyapb
14 vistas1 diapositiva
Voice Logger - Telephony Integration Solution at Aegis por
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at AegisNirmal Sharma
39 vistas1 diapositiva

Último(20)

Five Things You SHOULD Know About Postman por Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman33 vistas
SAP Automation Using Bar Code and FIORI.pdf por Virendra Rai, PMP
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdf
Virendra Rai, PMP23 vistas
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... por James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson85 vistas
STPI OctaNE CoE Brochure.pdf por madhurjyapb
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdf
madhurjyapb14 vistas
Voice Logger - Telephony Integration Solution at Aegis por Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma39 vistas
The details of description: Techniques, tips, and tangents on alternative tex... por BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada127 vistas
Transcript: The Details of Description Techniques tips and tangents on altern... por BookNet Canada
Transcript: The Details of Description Techniques tips and tangents on altern...Transcript: The Details of Description Techniques tips and tangents on altern...
Transcript: The Details of Description Techniques tips and tangents on altern...
BookNet Canada136 vistas
HTTP headers that make your website go faster - devs.gent November 2023 por Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn22 vistas
Business Analyst Series 2023 - Week 3 Session 5 por DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10248 vistas
AMAZON PRODUCT RESEARCH.pdf por JerikkLaureta
AMAZON PRODUCT RESEARCH.pdfAMAZON PRODUCT RESEARCH.pdf
AMAZON PRODUCT RESEARCH.pdf
JerikkLaureta26 vistas
Ransomware is Knocking your Door_Final.pdf por Security Bootcamp
Ransomware is Knocking your Door_Final.pdfRansomware is Knocking your Door_Final.pdf
Ransomware is Knocking your Door_Final.pdf
Security Bootcamp55 vistas
Empathic Computing: Delivering the Potential of the Metaverse por Mark Billinghurst
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the Metaverse
Mark Billinghurst478 vistas
Piloting & Scaling Successfully With Microsoft Viva por Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
Richard Harbridge12 vistas

Lecture_Oct26.pptx

  • 2. • The searching algorithms are used to search or find one or more than one element from a dataset. These type of algorithms are used to find elements from a specific data structures. • Searching may be sequential or not. If the data in the dataset are random, then we need to use sequential searching. Otherwise we can use other different techniques to reduce the complexity. • Search algorithm is any algorithm which solves the search problem, namely, to retrieve information stored within some data structure, or calculated in the search space of a problem domain, either with discrete or continuous values.
  • 3. Searching Algorithms are designed to check for an element or retrieve an element from any data structure where it is stored. Based on the type of search operation, these algorithms are generally classified into two categories: • Sequential Search: In this, the list or array is traversed sequentially and every element is checked. For example: Linear Search. • Interval Search: These algorithms are specifically designed for searching in sorted data-structures. These type of searching algorithms are much more efficient than Linear Search as they repeatedly target the center of the search structure and divide the search space in half. For Example: Binary Search. •
  • 4. Linear Search • a linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.[1] • A linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. If each element is equally likely to be searched, then linear search has an average case of n+1/2 comparisons, but the average case can be affected if the search probabilities for each element vary. Linear search is rarely practical because other search algorithms and schemes, such as the binary search algorithm and hash tables, allow significantly faster searching for all but short lists.[2]
  • 5. Time – Space Complexity Worst complexity: O(n) Average complexity: O(n) Space complexity: O(1) Worst-case space complexity: O(1) iterative Average performance: O(n/2)
  • 6. • A linear search sequentially checks each element of the list until it finds an element that matches the target value. If the algorithm reaches the end of the list, the search terminates unsuccessfully.[1] Basic algorithm • Given a list L of n elements with values or records L0 .... Ln−1, and target value T, the following subroutine uses linear search to find the index of the target T in L.[3] • Set i to 0. • If Li = T, the search terminates successfully; return i. • Increase i by 1. • If i < n, go to step 2. Otherwise, the search terminates unsuccessfully.
  • 7. With a sentinel • The basic algorithm above makes two comparisons per iteration: one to check if Li equals T, and the other to check if i still points to a valid index of the list. By adding an extra record Ln to the list (a sentinel value) that equals the target, the second comparison can be eliminated until the end of the search, making the algorithm faster. The search will reach the sentinel if the target is not contained within the list.[4] • Set i to 0. • If Li = T, go to step 4. • Increase i by 1 and go to step 2. • If i < n, the search terminates successfully; return i. Else, the search terminates unsuccessfully.
  • 8. In an ordered table • If the list is ordered such that L0 ≤ L1 ... ≤ Ln−1, the search can establish the absence of the target more quickly by concluding the search once Li exceeds the target. This variation requires a sentinel that is greater than the target.[5] • Set i to 0. • If Li ≥ T, go to step 4. • Increase i by 1 and go to step 2. • If Li = T, the search terminates successfully; return i. Else, the search terminates unsuccessfully.
  • 11. Homework: Use Linear search to Search for: 1001 and 904 2, 13, 45, 67, 98, 101, 456, 501, 602, 899, 904, 1000
  • 12. Binary Search Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form. Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned. If the middle item is greater than the item, then the item is searched in the sub- array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the right of the middle item. This process continues on the sub- array as well until the size of the subarray reduces to zero.
  • 13. Binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. Time- Space Complexity • Worst complexity: O(log n) • Average complexity: O(log n) • Best complexity: O(1) • Space complexity: O(1) • Data structure: Array
  • 14. How Binary Search Works? For a binary search to work, it is mandatory for the target array to be sorted. We shall learn the process of binary search with a pictorial example. The following is our sorted array and let us assume that we need to search the location of value 31 using binary search.
  • 15. First, we shall determine half of the array by using this formula − mid = low + ((high - low) / 2 ) Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
  • 16. Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the value at location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted array, so we also know that the target value must be in the upper portion of the array.
  • 17. We change our low to mid + 1 and find the new mid value again. low = mid + 1 low = 4 + 1 mid = low + ((high - low) / 2) mid = 5 + ((9 – 5)/2) mid = 5+2 = 7 Our new mid is 7 now. We compare the value stored at location 7 with our target value 31. The value stored at location 7 is not a match, rather it is more than what we are looking for. So, the value must be in the lower part from this location.
  • 18. Hence, we calculate the mid again. This time it is 5. low = mid + 1 low = 4 + 1 mid = low + ((high - low) / 2) mid = 5 + ((6 – 5)/2) mid = 5+ 0.5 = 5.5 or 5 We compare the value stored at location 5 with our target value. We find that it is a match.
  • 25. Jump Search • Jump Search Algorithm is a relatively new algorithm for searching an element in a sorted array. • The fundamental idea behind this searching technique is to search fewer number of elements compared to linear search algorithm (which scans every element in the array to check if it matches with the element being searched or not). This can be done by skipping some fixed number of array elements or jumping ahead by fixed number of steps in every iteration.
  • 26. Jump Search is a searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some elements in place of searching all elements. • For example, suppose we have an array arr[] of size n and block (to be jumped) size m. Then we search at the indexes arr[0], arr[m], arr[2m]…..arr[km] and so on. Once we find the interval (arr[km] < x < arr[(k+1)m]), we perform a linear search operation from the index km to find the element x.
  • 27. Let’s consider the following array: (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610). Length of the array is 16. Jump search will find the value of 55 with the following steps assuming that the block size to be jumped is 4. STEP 1: Jump from index 0 to index 4; STEP 2: Jump from index 4 to index 8; STEP 3: Jump from index 8 to index 12; STEP 4: Since the element at index 12 is greater than 55 we will jump back a step to come to index 8. STEP 5: Perform linear search from index 8 to get the element 55.
  • 28. What is the optimal block size to be skipped? In the worst case, we have to do n/m jumps and if the last checked value is greater than the element to be searched for, we perform m-1 comparisons more for linear search. Therefore the total number of comparisons in the worst case will be ((n/m) + m-1). The value of the function ((n/m) + m-1) will be minimum when m = √n. Therefore, the best step size is m = √n.
  • 29. How to choose the block or Jump size? If the jump size is 1, then it will be a linear search. Hence we usually take the jump size of SQRT(n). By doing so, in the worst case we do n/k jumps. Now let us understand the algorithm with help of example: 1, 1, 2, 3, 4, 10, 15, 20, 75, 80, 92, 95, 100 Search Key = 80 N = 13 Jump size = SQRT(13) = 3
  • 31. Homework: Given: - 1, 0, 5, 9, 23, 45, 55, 67, 78, 88, 99, 101, 105, 206 Perform Jump Search to search for: 206
  • 32. Interpolation search • Interpolation search is an improvement over binary search. • Binary Search always checks the value at middle index. But, interpolation search may check at different locations based on the value of element being searched. • For interpolation search to work efficiently the array elements/data should be sorted and uniformly distributed. • Steps: • A - Array of elements, e - element to be searched, pos - current position • Make start = 0 & end = n-1 • calculate position ( pos ) to start searching at using formula:
  • 33. • If A[pos] == e , element found at index pos. • Otherwise if e > A[pos] we make start = pos + 1 • Else if e < A[pos] we make end = pos -1 • Do stpes 3, 4, 5, 6 While : start <= end && e >= A[start] && e =< A[end] • start <= end - that is until we have elements in the sub-array. • e >= A[start] - element we are looking for is greater than or equal to the starting element of sub-array we are looking in. • e =< A[end] - element we are looking for is less than or equal to the last element of sub- array we are looking in.
  • 34. • Example if we are looking for element 4 in the given array.
  • 35. Given: - 1, 0, 5, 9, 23, 45, 55, 67, 78, 88, 99, 101, 105, 206 Perform Interpolation Search to search for: 99