3.1 Searching Techniques
3.1 Searching Techniques
3.1 Searching Techniques
What is Searching?
Searching in data structure refers to the process of
finding the required information from a collection of
items stored as elements.
A Search is said to be successful or unsuccessful
depending upon whether the element that is being
searched is found or not.
Introduction to Searching Algorithms
Not even a single day pass, when we do not have to search
for something in our day to day life, car keys, books, pen,
mobile charger and what not.
Same is the life of a computer, there is so much data stored
in it, that whenever a user asks for some data, computer has
to search it's memory to look for the data and make it
available to the user.
Around 80% of CPU cycle wasted in searching process.
Well, to search an element in a given list, there are three
popular algorithms available:
Linear Search
Binary Search
Index Sequential Search
Linear Search
Linear search is a very basic and simple search algorithm.
In Linear search, we search an element or value in a given
list by traversing the list from the starting, till the desired
element or value is found.
It compares the element to be searched with all the
elements present in the list and when the element
is matched successfully, it returns the index of the element
in the array, else it return -1.
Let the searched element is 33
Linear Search Example
If you are asked to find the name of a person having phone number
say “1234” with the help of telephone directory.
Since the telephone directory is sorted by name not by phone
number, we have to go through each and every number of the
directory.
Advantage:
If the first number in the directory is the number you were
searching for, then lucky you!!
Since you have found it on very first page, now it is not important
for you that how many pages are there in the directory.
Disadvantage:
If the number you are searching for is the last number of directory
or it is not in the directory at all.
In that case you have to search the whole directory.
Features of Linear Search Algorithm
It is used for unsorted and unordered small list of
elements.
It has a time complexity of O(n), which means the time is
linearly dependent on the number of elements, which is
not bad, but not that good too.
It has a very simple implementation.
Linear Search is suitable if the list contains few elements.
https://www.cs.usfca.edu/~galles/visualization/Search.html
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
Implementing Linear Search
int linearSearch(int a[], int n, int val) {
for (int i = 0; i < n; i++) // Going through array sequentially
{
if (a[i] == val)
return i+1;
}
return -1;
}
int main() {
int a[] = {70, 40, 30, 11, 57, 41, 25, 14, 52}; // given array
int val = 41; // value to be searched
int n = sizeof(a) / sizeof(a[0]); // size of array
int loc = linearSearch(a, n, val); // Store result
if (loc == -1)
printf("\nElement is not present in the array");
else
printf("\nElement is present at %d position of array", loc);
return 0;
}
Time and Space Complexity
Best case complexity: O(1)
Worst case complexity: O(n)
Average complexity: O(n)
Space complexity: O(1)
Average performance: O(n/2)
GATE CS 1996
The average number of key comparisons done in a
successful sequential search in a list of length n is:
log n
(n-1)/2
n/2
(n+1)/2
ISRO CS 2011
Number of comparisons required for an unsuccessful search
of an element in a sequential search, organized, fixed
length, symbol table of length L is:
(L+1)/2
L
L/2
L+1
Binary Search Algorithm
Binary Search is applied on the sorted array or list of
large size.
It's time complexity of O(log n) makes it very fast as
compared to other searching algorithms.
The only limitation is that the array or list of elements must
be sorted for the binary search algorithm to work on it.
Implementing Binary Search Algorithm
Following are the steps of implementation of Binary Search:
Step-1: Find the middle index of the list.
Step-2: If the target value is equal to the middle element of the array,
then return the index of the middle element.
Step-3: If not, then compare the middle element with the target value,
If the target value is greater than the number in the middle index,
then pick the elements to the right of the middle index, and start with
Step-1.
If the target value is less than the number in the middle index, then
pick the elements to the left of the middle index, and start with Step-
1.
Step-4: When a match is found, return the index of the element
matched.
Step-5: If no match is found, then return -1
Time Complexity
ISRO CS 2017 - May
Which of the following is correct recurrence for worst case
of Binary Search?
T(n) = 2T(n/2) + O(1) and T(1) = T(0) = O(1)
T(n) = T(n-1) + O(1) and T(1) = T(0) = O(1)
T(n) = T(n/2) + O(1) and T(1) = T(0) = O(1)
T(n) = T(n-2) + O(1) and T(1) = T(0) = O(1)
ISRO CS 2014
Suppose there are 11 items in sorted order in an array. How
many searches are required on the average, if binary
search is employed and all searches are successful in
finding the item?
3.46
3.00
3.21
2.81
UGC NET CS 2014 Dec - III
Suppose that we have numbers between 1 and 1000 in a
binary search tree and we want to search for the number
365. Which of the following sequences could not be the
sequence of nodes examined?
4, 254, 403, 400, 332, 346, 399, 365
926, 222, 913, 246, 900, 260, 364, 365
927, 204,913, 242, 914, 247, 365
4, 401, 389, 221, 268, 384, 383, 280, 365
Indexed Sequential Search
Index search is special search. This search method is used to
search a record in a file. Searching a record refers to the
searching of location loc in memory where the file is stored.
Indexed search searches the record with a given key value
relative to a primary key field. This search method is
accomplished by the use of pointers.
Index helps to locate a particular record with less time. Indexed
sequential files use the principal of index creation.
For Example, Directory, book etc. In this type of file
organization, the records are organized in a sequence and an
index table is used to speed up the access to records without
searching the entire file. The records of the file may be sorted in
random sequence but the index table is in sorted sequence on the
key field. Thus the file can be processed randomly as well as
sequentially.
Advantage
More efficient.
Time required is less.
ISRO CS 2016
A clustering index is defined on the fields which are of
type:
non-key and ordering
non-key and non-ordering
key and ordering
key and non-ordering
GATE-CS-2015 (Set 1)
A file is organized so that the ordering of data records is
the same as or close to the ordering of data entries in some
index. Then that index is called:
Dense
Sparse
Clustered
Unclustered
Hashing Techniques
In data structures,
There are several searching techniques like linear search,
binary search, search trees etc.
In these techniques, time taken to search any particular
element depends on the total number of elements.