SlideShare a Scribd company logo
Unit III
Searching and Sorting
Searching Algorithms
• Consider a database of banking system where information of all
customers is stored, such as name, address and account number etc.
If a manager wants to search for a record of a particular customer, he
has to look for that record from among all records that has been
stored in the database.
• This process of looking up for a particular record in a database is
referred as searching.
• 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:
1. Sequential Search: In this, the list or array is traversed sequentially
and every element is checked. For example: Linear search
2. 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 centre of the search structure and divide the
search space in half. For Example: Binary search
Searching Techniques are generally classified into:
1. Sequential Search
2. Sentinel Search
3. Binary Search
4. Fibonacci Search
5. Indexed Sequential search
Sequential/Linear Search
• Linear search is a very simple search algorithm. In this type of search, a
sequential search is made over all items one by one. Every item is checked
and if a match is found then that particular item is returned, otherwise the
search continues till the end of the data collection.
• A simple approach is to do a linear search, i.e
1. Start from the leftmost element of arr[] and one by one compare x with
each element of arr[]
2. If x matches with an element, return the index.
3. If x doesn’t match with any of elements, return -1.
Algorithm
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
Pseudocode
procedure linear_search (list, value)
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure
• Problem: Given an array arr[] of n elements, write a
function to search a given element x in arr[].
• Examples :
The time complexity of the above algorithm is O(n).
Sentinel Search
• It is a variant of sequential search.
• Sentinel Linear Search as the name suggests is a type of Linear search where the
number of comparisons are reduced as compared to a traditional linear search.
• It is another way to reduce the overhead is to eliminate all checking of the loop index.
This can be done by inserting the desired item itself as a sentinel value at the far end of
the list
• When a linear search is performed on an array of size N then in the worst case a total
of N comparisons are made when the element to be searched is compared to all the
elements of the array and (N + 1) comparisons are made for the index of the element
to be compared so that the index is not out of bounds of the array which can be
reduced in a Sentinel Linear Search.
• In this search, the element to be searched is added at the end of the list and then the
linear search is performed on the array without checking whether the current index is
inside the index range of the array or not ,because the element to be searched will
definitely be found inside the array even if it was not present in the original array since
the last element is the search element only.
• So, the index to be checked will never be out of bounds of the array. The number of
comparisons in the worst case here will be (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
• 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.
1. Set i to 0.
2. If Li = T, the search terminates successfully; return i.
3. Increase i by 1.
4. 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.
• 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.
Examples:
Input: arr[] = {10, 20, 180, 30, 60, 50, 110, 100, 70}, x = 180
Output: 180 is present at index 2
Input: arr[] = {10, 20, 180, 30, 60, 50, 110, 100, 70}, x = 90
Output: Not found
Binary Search
• The sequential search algorithm is very slow.
• If we have an array of 1000 elements, we must make 1000 comparisons in the
worst case.
• The binary search starts by testing the data in the element at the middle of
the array to determine if the target is in the first or the second half of the list.
• mid = (begin + end) / 2
• If it is in the first half, we do not need to check the second half.
• If it is in the second half, we do not need to test the first half.
• In other words, we eliminate half the list from further consideration with just
one comparison. We repeat this process, eliminating half of the remaining list
with each test, until we find the target or determine that it is not in the list.
• To find the middle of the list, we need three variables: one to identify the
beginning of the list, one to identify the middle of the list, and one to identify
the end of the list.
• We analyze two cases here: the target is in the list and the target is not in the
list.
Searching techniques
• 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.
• We basically ignore half of the elements just after one comparison.
1. Compare x with the middle element.
2. If x matches with middle element, we return the mid index.
3. Else If x is greater than the mid element, then x can only lie in right half sub
array after the mid element. So we recur for right half.
4. Else (x is smaller) recur for the left half.
• The idea of binary search is to use the information that the array is sorted
and reduce the time complexity to O(Log n)
Searching techniques
Searching techniques
Pseudocode
Fibonacci series
• a series of numbers in which each number
( Fibonacci number ) is the sum of the two
preceding numbers.
• First few Fibonacci Numbers are 0, 1, 1, 2, 3, 5,
8, 13, 21, 34, 55, 89, 144, …
Fibonacci Search
 Fibonacci Search is a comparison-based technique that uses Fibonacci
numbers to search an element in a sorted array.
 Similarities with Binary Search:
• Works for sorted arrays
• A Divide and Conquer Algorithm.
• Has Log n time complexity.
 Differences with Binary Search:
• Fibonacci Search divides given array in unequal parts
• Binary Search uses division operator to divide range. Fibonacci Search
doesn’t use /, but uses + and -. The division operator may be costly on some
CPUs.
• Fibonacci Search examines relatively closer elements in subsequent steps.
So when input array is big that cannot fit in CPU cache or even in RAM,
Fibonacci Search can be useful.
 Background:
Fibonacci Numbers are recursively defined as F(n) = F(n-1) + F(n-2), F(0) = 0,
F(1) = 1. First few Fibonacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
144, …
Fibonacci Search
Given a sorted array arr[] of size n and an
element x to be searched in it. Return index of
x if it is present in array else return -1.
Examples:
Algorithm:
Let the searched element be x.
• The idea is to first find the smallest Fibonacci number that is greater than or equal
to the length of given array.
• Let the found Fibonacci number be fib (m’th Fibonacci number).
• We use (m-2)’th Fibonacci number as the index (If it is a valid index).
• Let (m-2)’th Fibonacci Number be i, we compare arr[i] with x, if x is same, we
return i. Else if x is greater, we recur for subarray after i, else we recur for subarray
before i.
• Below is the complete algorithm
Let arr[0..n-1] be the input array and element to be searched be x.
1. Find the smallest Fibonacci Number greater than or equal to n. Let this number be
fibM [m’th Fibonacci Number]. Let the two Fibonacci numbers preceding it be
fibMm1 [(m-1)’th Fibonacci Number] and fibMm2 [(m-2)’th Fibonacci Number].
2. While the array has elements to be inspected:
1. Compare x with the last element of the range covered by fibMm2
2. If x matches, return index
3. Else If x is less than the element, move the three Fibonacci variables
two Fibonacci down, indicating elimination of approximately rear two-
third of the remaining array.
4. Else x is greater than the element, move the three Fibonacci variables
one Fibonacci down. Reset offset to index. Together these indicate
elimination of approximately front one-third of the remaining array.
3. Since there might be a single element remaining for comparison, check if
fibMm1 is 1. If Yes, compare x with that remaining element. If match, return
index.
Illustration:
Let us understand the algorithm with below example:
Illustration assumption:
•1-based indexing.
•Target element x is 85.
•Length of array n = 11.
•Smallest Fibonacci number greater than or equal to 11 is 13.
•As per our illustration, fibMm2 = 5, fibMm1 = 8, and fibM = 13.
•Another implementation detail is the offset variable (zero initialized).
• It marks the range that has been eliminated, starting from the front. We will update
it time to time.
• Now since the offset value is an index and all indices including it and below it
have been eliminated, it only makes sense to add something to it. Since
fibMm2 marks approximately one-third of our array, as well as the indices it
marks are sure to be valid ones, we can add fibMm2 to offset and check the
element at index i = min(offset + fibMm2, n).
Visualization:
• Observations:
Below observation is used for range
elimination, and hence for the O(log(n))
complexity.
• Time Complexity analysis:
The worst case will occur when we have our target in the larger (2/3)
fraction of the array, as we proceed to find it. In other words, we are
eliminating the smaller (1/3) fraction of the array every time. We call once
for n, then for(2/3) n, then for (4/9) n and henceforth.
• Consider that:
Indexed Sequential Search
• It is another method to improve the efficiency of searching in a
sorted list. An auxiliary table called an index is set aside in addition
to the sorted file itself.
• In this searching method, first of all, an index file is created, that
contains some specific group or division of required record when
the index is obtained, then the partial indexing takes less time
cause it is located in a specified group.
• But it needs extra space; this method is also known as indexed
sequential method. Let r, k and key be three input values of this
algorithm. Other values are kindex be an array of keys in the index,
and let pindex be the array of pointers with in the index.
• Indxsize size of the index table. N number of records in the main
table.
• Note: When the user makes a request for specific records it will
find that index group first where that specific record is recorded.
• Characteristics of Indexed Sequential Search:
• In Indexed Sequential Search a sorted index is set aside
in addition to the array. the partial searching takes less
time since it is to be located in the group/bucket
specified by the index.
• Each element in the index points to a block of elements
in the array or another expanded index.
• The index is searched 1st then the array and guides the
search in the array.
• Note: Indexed Sequential Search actually does the
indexing multiple time, like creating the index of an
index.
• For example program creates an index file for the
employee records by grouping the records and
then locates the required key by searching the
index first and then returns the required record.
• The advantage of indexed sequential over
sequential is that here we can achieve both
sequential as well as Random access (i.e. jump
directly to a record from the index value).
Moreover, it is faster than sequential method.
Explanation by diagram “Indexed Sequential Search”:
• If the table is so large a single index cannot be sufficient to achieve efficiency. So we
can maintain secondary index.
•The secondary index acts as an index to the primary index. Which will points entry to
the sequential table.

More Related Content

PPT
Fibonacci search
PPTX
B. SC CSIT Computer Graphics Lab By Tekendra Nath Yogi
PPTX
Karatsuba algorithm for fast mltiplication
PPT
Counting Sort and Radix Sort Algorithms
PDF
Greedy algorithm activity selection fractional
PPT
Lower bound
PPTX
Divide and conquer - Quick sort
PPTX
Stack & Queue using Linked List in Data Structure
Fibonacci search
B. SC CSIT Computer Graphics Lab By Tekendra Nath Yogi
Karatsuba algorithm for fast mltiplication
Counting Sort and Radix Sort Algorithms
Greedy algorithm activity selection fractional
Lower bound
Divide and conquer - Quick sort
Stack & Queue using Linked List in Data Structure

What's hot (20)

PPTX
Quick sort
PDF
Sorting Algorithms
PPTX
Insertion Sorting
PPT
Data Structures- Part5 recursion
PDF
Algorithms Lecture 7: Graph Algorithms
PPT
PPTX
Recursion
PPTX
Demonstrate interpolation search
PPT
B trees in Data Structure
PPTX
PPTX
Insertion sort algorithm power point presentation
PDF
Algorithms Lecture 2: Analysis of Algorithms I
PPTX
PPT
Binary Search
PPT
Queue data structure
PPTX
Binary Tree in Data Structure
PPTX
frames.pptx
PPTX
Disjoint sets union, find
PPTX
Searching techniques in Data Structure And Algorithm
PPT
Algorithm: Quick-Sort
Quick sort
Sorting Algorithms
Insertion Sorting
Data Structures- Part5 recursion
Algorithms Lecture 7: Graph Algorithms
Recursion
Demonstrate interpolation search
B trees in Data Structure
Insertion sort algorithm power point presentation
Algorithms Lecture 2: Analysis of Algorithms I
Binary Search
Queue data structure
Binary Tree in Data Structure
frames.pptx
Disjoint sets union, find
Searching techniques in Data Structure And Algorithm
Algorithm: Quick-Sort
Ad

Similar to Searching techniques (20)

PPTX
Lecture_Oct26.pptx
PPTX
unit II_2_i.pptx
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPTX
Dsa – data structure and algorithms searching
DOCX
MODULE 5-Searching and-sorting
PPT
Searching in c language
PDF
Searching
PPTX
Data structure Unit - II Searching and Sorting.pptx
PDF
PPTX
Array ADT(Abstract Data Type)|Data Structure
PPTX
Data Structures Unit 2 FINAL presentation.pptx
PPTX
data_structure_Chapter two_computer.pptx
PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
PDF
Linear and Binary Search
PPTX
Searching and Sorting Algorithms in Data Structures
PDF
advanced searching and sorting.pdf
PPTX
Presentation on Searching and Sorting in C language.pptx
PPTX
Linear and binary search
PPTX
Searching & Algorithms IN DATA STRUCTURES
Lecture_Oct26.pptx
unit II_2_i.pptx
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Dsa – data structure and algorithms searching
MODULE 5-Searching and-sorting
Searching in c language
Searching
Data structure Unit - II Searching and Sorting.pptx
Array ADT(Abstract Data Type)|Data Structure
Data Structures Unit 2 FINAL presentation.pptx
data_structure_Chapter two_computer.pptx
Chapter #3 (Searchinmmmg ALgorithm).pptx
Linear and Binary Search
Searching and Sorting Algorithms in Data Structures
advanced searching and sorting.pdf
Presentation on Searching and Sorting in C language.pptx
Linear and binary search
Searching & Algorithms IN DATA STRUCTURES
Ad

Recently uploaded (20)

PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPT
Total quality management ppt for engineering students
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
Current and future trends in Computer Vision.pptx
PDF
Visual Aids for Exploratory Data Analysis.pdf
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
Abrasive, erosive and cavitation wear.pdf
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
Analyzing Impact of Pakistan Economic Corridor on Import and Export in Pakist...
PDF
PPT on Performance Review to get promotions
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
R24 SURVEYING LAB MANUAL for civil enggi
Total quality management ppt for engineering students
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Current and future trends in Computer Vision.pptx
Visual Aids for Exploratory Data Analysis.pdf
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
Abrasive, erosive and cavitation wear.pdf
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Categorization of Factors Affecting Classification Algorithms Selection
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Analyzing Impact of Pakistan Economic Corridor on Import and Export in Pakist...
PPT on Performance Review to get promotions
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf

Searching techniques

  • 2. Searching Algorithms • Consider a database of banking system where information of all customers is stored, such as name, address and account number etc. If a manager wants to search for a record of a particular customer, he has to look for that record from among all records that has been stored in the database. • This process of looking up for a particular record in a database is referred as searching. • 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: 1. Sequential Search: In this, the list or array is traversed sequentially and every element is checked. For example: Linear search 2. 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 centre of the search structure and divide the search space in half. For Example: Binary search
  • 3. Searching Techniques are generally classified into: 1. Sequential Search 2. Sentinel Search 3. Binary Search 4. Fibonacci Search 5. Indexed Sequential search
  • 4. Sequential/Linear Search • Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection. • A simple approach is to do a linear search, i.e 1. Start from the leftmost element of arr[] and one by one compare x with each element of arr[] 2. If x matches with an element, return the index. 3. If x doesn’t match with any of elements, return -1.
  • 5. Algorithm Linear Search ( Array A, Value x) Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit Pseudocode procedure linear_search (list, value) for each item in the list if match item == value return the item's location end if end for end procedure
  • 6. • Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[]. • Examples : The time complexity of the above algorithm is O(n).
  • 7. Sentinel Search • It is a variant of sequential search. • Sentinel Linear Search as the name suggests is a type of Linear search where the number of comparisons are reduced as compared to a traditional linear search. • It is another way to reduce the overhead is to eliminate all checking of the loop index. This can be done by inserting the desired item itself as a sentinel value at the far end of the list • When a linear search is performed on an array of size N then in the worst case a total of N comparisons are made when the element to be searched is compared to all the elements of the array and (N + 1) comparisons are made for the index of the element to be compared so that the index is not out of bounds of the array which can be reduced in a Sentinel Linear Search. • In this search, the element to be searched is added at the end of the list and then the linear search is performed on the array without checking whether the current index is inside the index range of the array or not ,because the element to be searched will definitely be found inside the array even if it was not present in the original array since the last element is the search element only. • So, the index to be checked will never be out of bounds of the array. The number of comparisons in the worst case here will be (N + 2).
  • 8. • 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 • 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. 1. Set i to 0. 2. If Li = T, the search terminates successfully; return i. 3. Increase i by 1. 4. 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. • 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.
  • 9. Examples: Input: arr[] = {10, 20, 180, 30, 60, 50, 110, 100, 70}, x = 180 Output: 180 is present at index 2 Input: arr[] = {10, 20, 180, 30, 60, 50, 110, 100, 70}, x = 90 Output: Not found
  • 10. Binary Search • The sequential search algorithm is very slow. • If we have an array of 1000 elements, we must make 1000 comparisons in the worst case. • The binary search starts by testing the data in the element at the middle of the array to determine if the target is in the first or the second half of the list. • mid = (begin + end) / 2 • If it is in the first half, we do not need to check the second half. • If it is in the second half, we do not need to test the first half. • In other words, we eliminate half the list from further consideration with just one comparison. We repeat this process, eliminating half of the remaining list with each test, until we find the target or determine that it is not in the list. • To find the middle of the list, we need three variables: one to identify the beginning of the list, one to identify the middle of the list, and one to identify the end of the list. • We analyze two cases here: the target is in the list and the target is not in the list.
  • 12. • 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. • We basically ignore half of the elements just after one comparison. 1. Compare x with the middle element. 2. If x matches with middle element, we return the mid index. 3. Else If x is greater than the mid element, then x can only lie in right half sub array after the mid element. So we recur for right half. 4. Else (x is smaller) recur for the left half. • The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n)
  • 16. Fibonacci series • a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. • First few Fibonacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
  • 17. Fibonacci Search  Fibonacci Search is a comparison-based technique that uses Fibonacci numbers to search an element in a sorted array.  Similarities with Binary Search: • Works for sorted arrays • A Divide and Conquer Algorithm. • Has Log n time complexity.  Differences with Binary Search: • Fibonacci Search divides given array in unequal parts • Binary Search uses division operator to divide range. Fibonacci Search doesn’t use /, but uses + and -. The division operator may be costly on some CPUs. • Fibonacci Search examines relatively closer elements in subsequent steps. So when input array is big that cannot fit in CPU cache or even in RAM, Fibonacci Search can be useful.  Background: Fibonacci Numbers are recursively defined as F(n) = F(n-1) + F(n-2), F(0) = 0, F(1) = 1. First few Fibonacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
  • 18. Fibonacci Search Given a sorted array arr[] of size n and an element x to be searched in it. Return index of x if it is present in array else return -1. Examples:
  • 19. Algorithm: Let the searched element be x. • The idea is to first find the smallest Fibonacci number that is greater than or equal to the length of given array. • Let the found Fibonacci number be fib (m’th Fibonacci number). • We use (m-2)’th Fibonacci number as the index (If it is a valid index). • Let (m-2)’th Fibonacci Number be i, we compare arr[i] with x, if x is same, we return i. Else if x is greater, we recur for subarray after i, else we recur for subarray before i. • Below is the complete algorithm Let arr[0..n-1] be the input array and element to be searched be x. 1. Find the smallest Fibonacci Number greater than or equal to n. Let this number be fibM [m’th Fibonacci Number]. Let the two Fibonacci numbers preceding it be fibMm1 [(m-1)’th Fibonacci Number] and fibMm2 [(m-2)’th Fibonacci Number].
  • 20. 2. While the array has elements to be inspected: 1. Compare x with the last element of the range covered by fibMm2 2. If x matches, return index 3. Else If x is less than the element, move the three Fibonacci variables two Fibonacci down, indicating elimination of approximately rear two- third of the remaining array. 4. Else x is greater than the element, move the three Fibonacci variables one Fibonacci down. Reset offset to index. Together these indicate elimination of approximately front one-third of the remaining array. 3. Since there might be a single element remaining for comparison, check if fibMm1 is 1. If Yes, compare x with that remaining element. If match, return index.
  • 21. Illustration: Let us understand the algorithm with below example: Illustration assumption: •1-based indexing. •Target element x is 85. •Length of array n = 11. •Smallest Fibonacci number greater than or equal to 11 is 13. •As per our illustration, fibMm2 = 5, fibMm1 = 8, and fibM = 13. •Another implementation detail is the offset variable (zero initialized). • It marks the range that has been eliminated, starting from the front. We will update it time to time.
  • 22. • Now since the offset value is an index and all indices including it and below it have been eliminated, it only makes sense to add something to it. Since fibMm2 marks approximately one-third of our array, as well as the indices it marks are sure to be valid ones, we can add fibMm2 to offset and check the element at index i = min(offset + fibMm2, n).
  • 24. • Observations: Below observation is used for range elimination, and hence for the O(log(n)) complexity.
  • 25. • Time Complexity analysis: The worst case will occur when we have our target in the larger (2/3) fraction of the array, as we proceed to find it. In other words, we are eliminating the smaller (1/3) fraction of the array every time. We call once for n, then for(2/3) n, then for (4/9) n and henceforth. • Consider that:
  • 26. Indexed Sequential Search • It is another method to improve the efficiency of searching in a sorted list. An auxiliary table called an index is set aside in addition to the sorted file itself. • In this searching method, first of all, an index file is created, that contains some specific group or division of required record when the index is obtained, then the partial indexing takes less time cause it is located in a specified group. • But it needs extra space; this method is also known as indexed sequential method. Let r, k and key be three input values of this algorithm. Other values are kindex be an array of keys in the index, and let pindex be the array of pointers with in the index. • Indxsize size of the index table. N number of records in the main table. • Note: When the user makes a request for specific records it will find that index group first where that specific record is recorded.
  • 27. • Characteristics of Indexed Sequential Search: • In Indexed Sequential Search a sorted index is set aside in addition to the array. the partial searching takes less time since it is to be located in the group/bucket specified by the index. • Each element in the index points to a block of elements in the array or another expanded index. • The index is searched 1st then the array and guides the search in the array. • Note: Indexed Sequential Search actually does the indexing multiple time, like creating the index of an index.
  • 28. • For example program creates an index file for the employee records by grouping the records and then locates the required key by searching the index first and then returns the required record. • The advantage of indexed sequential over sequential is that here we can achieve both sequential as well as Random access (i.e. jump directly to a record from the index value). Moreover, it is faster than sequential method.
  • 29. Explanation by diagram “Indexed Sequential Search”:
  • 30. • If the table is so large a single index cannot be sufficient to achieve efficiency. So we can maintain secondary index. •The secondary index acts as an index to the primary index. Which will points entry to the sequential table.