Searching
Searching
Searching
1. Linear Search
2. Binary Search
3. Index Linear 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 array by traversing the array 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 array and when the element
is matched successfully, it returns the index of the element in the array, else it return -1.
Linear Search is applied on unsorted or unordered lists, when there are fewer elements in a list.
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
Binary Search is used with sorted array or list. In binary search, we follow the following steps:
1. We start by comparing the element to be searched with the element in the middle of the
list/array.
2. If we get a match, we return the index of the middle element.
3. If we do not get a match, we check whether the element to be searched is less or greater
than in value than the middle element.
4. If the element/number to be searched is greater in value than the middle number, then
we pick the elements on the right side of the middle element (as the list/array is sorted,
hence on the right, we will have all the numbers greater than the middle number), and
start again from the step 1.
5. If the element/number to be searched is lesser in value than the middle number, then we
pick the elements on the left side of the middle element, and start again from the step 1.
Binary Search is useful when there are large number of elements in an array and they are sorted.
So a necessary condition for Binary search to work is that the list/array should be sorted.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
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.
10 14 19 27 31 26 33 35 42 44
0 1 2 3 4 5 6 7 8 9
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.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
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.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
Hence, we calculate the mid again. This time it is 5.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
We compare the value stored at location 5 with our target value. We find that it is a match.
10 14 19 26 27 31 33 35 42 44
0 1 2 3 4 5 6 7 8 9
We conclude that the target value 31 is stored at location 5.
Binary search halves the searchable items and thus reduces the count of comparisons to be
made to very less numbers. Pseudocode
lowerBound = midPoint + 1 if
= midPoint - 1 if A[midPoint] = x
CODE:
// C program for Indexed Sequential Search
#include <stdio.h>
#include <stdlib.h>
// Storing element
elements[ind] = arr[i];
end = indices[i];
set = 1;
break;
}
}
if (set == 0) {
start = indices[GN - 1];
end = GN;
}
for (i = start; i <= end; i++) {
if (k == arr[i]) {
j = 1;
break;
}
}
if (j == 1)
printf("Found at index %d", i);
else
printf("Not found");
}
// Driver code
void main()
{
// Element to search
int k = 8;
indexedSequentialSearch(arr, n, k);
}