0% found this document useful (0 votes)
30 views

What is Searching

Uploaded by

anujbhagat031
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

What is Searching

Uploaded by

anujbhagat031
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

What is Searching?

Searching is the process of finding a given value position in a list of values. It decides
whether a search key is present in the data or not. It can be done on internal data
structure or on external data structure.
Searching Techniques
To search an element in a given array, it can be done in following ways:

1. Sequential Search
2. Jump Search
3. Binary Search
4. Interpolation Search

1. Sequential Search
Sequential search is also called as Linear Search. Sequential search starts at the
beginning of the list and checks every element of the list. It is a basic and simple
search algorithm. Sequential search compares the element with all the other elements
given in the list. If the element is matched, it returns the value index, else it returns -
1.

The above figure shows how sequential search works. It searches an element or value
from an array till the desired element or value is not found. If we search the element
25, it will go step by step in a sequence order. It searches in a sequence order.
Sequential search is applied on the unsorted or unordered list when there are fewer
elements in a list.
Algorithim:
Linear Search ( Array A, Value x)

Step 1: Set i to 0
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
Time Complexity:
Best Case Avg Case Worst Case
O(1) O(n) O(n)
Best case : if searching element stay at the 0th position of the list
Worst case: if searching element stay at last position
Avg Case: if searching element stay at middle
Advantages and disadvantages of the linear search
Advantages:

 The linear search is simple - It is very easy to understand and implement;


 It does not require the data in the array to be stored in any particular order.

Disadvantages:

 The linear search is inefficient - If the array being searched contains 20,000
elements, the algorithm will have to look at all 20,000 elements in order to find a
value in the last element. In an average case, an item is just as likely to be found
near the beginning of the array as near the end. Typically, for an array of N items,
the linear search will locate an item in N/2 attempts. If an array has 50,000
elements, the linear search will make a comparison with 25,000 of them in a typical
case. This is assuming that the search item is consistently found in the array. N/2 is
the average number of comparisons. The maximum number of comparisons
is always N.

2. Jump Search
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[k*m] and so on. Once we
find the interval (arr[k*m] < x < arr[(k+1)*m]), we perform a linear search operation
from the index „k*m‟ 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 16 is greater than 55 we will jump back a step to
come to index 9.
STEP 5: Perform linear search from index 9 to get the element 55.

Advantages - Jump Search


 Jump search algorithm is more efficient in case of finding a element 600 out of
625 elements in an array.
 Jump search algorithm takes 25 iteration to find a element 600 out of 625
elements in an array.
 Whereas Linear search algorithm takes 600 iteration to find a element 600 out
of 625 elements in an array.
 Whereas Binary search algorithm takes 19 iteration to find a element 600 out
of 625 elements in an array but complexity in calculation is very tough as
compared to jump search algorithm.

Disadvantages - Jump Search


 Jump search algorithm is not preferable for unsorted list or array.
 Jump search algorithm is not preferable for Linked list.

Algorithm of Jump_search
Step1. int p=sqrt(n)
Step2. for(int i=0;i<p;i++)
Step2.1 if(a[i*p]<x<a[(i+1)*p])
Step2.1.1 for(int j=i*p;j<(i+1)*p;j++)
Step 2.1.1.1 if(x==a[j]) then f=1 and exit from loop
Step3. If f==0 then item, not found
Step4. Else item found
Time Complexity
Best Case Avg Case Worst Case
O(1) O( 𝑛) O( 𝑛)
In the worst / Average case , we have to do n/m jumps and m-1 comparison for linear
𝑛
search so total number of comparison = + 𝑚 − 1 but we know that m=√n
𝑚

𝑛
So + 𝑛 − 1 = 𝑛 + 𝑛 − 1 = 2 𝑛 − 1 ≡ 𝑂( 𝑛)
𝑛

Best case : if searching element stay at the 0th position of the list

Worst case: if searching element stay at last position

Avg Case: if searching element stay at middle

3. Binary Search:
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.
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
mid = low + (high - low) / 2
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.

We compare the value stored at location 5 with our target value. We find that it is a match.
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.

Algorithm
The pseudocode of binary search algorithms should look like this −
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched

Set lowerBound = 0
Set upperBound = n-1

while x not found


if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
Advantages:

 Compared to linear search ,binary search is much faster.


 Compared to jump search ,binary search is much faster if number of element of list is
very .
Disadvantages:

 It’s more complicated than linear search, and is overkill for very small numbers of
elements.
 It works only on lists that are sorted and kept sorted. That is not always feasable,
especially if elements are constantly being added to the list.
 It can not be implemented on Linked List.
 There are even faster search methods available, such as hash lookups. However a hash
lookup requires the elements to be organized in a much more complicated data
structure (a hash table, not a list).
Time Complexity
Best Case Avg Case Worst Case
O(1) O(log(n)) O(log(n))
Best case : if searching element stay at the middle of the list
Worst case: if searching element stay at 1st or last position
Time Complexity Calculation
𝑛
T(n)= T( )+1 [T(n/2) time is taken to search the element into sub list and 1 is for
2
comparing the value]
𝑛 𝑛
Now 𝑇 =𝑇 +1
2 4
𝑛 𝑛 𝑛
T(n)= 𝑇 +1= 𝑇 +1+1 [ replace the value of 𝑇 ]
2 4 2
𝑛
= 𝑇( 2 )+2
2
𝑛 𝑛
Now 𝑇( )= 𝑇 +1
4 8
𝑛 𝑛
So T(n)= T( )+1+2= 𝑇( 3 )+3 [replace the value of T(n/4)]
8 2
th 𝑛
So for k term T(n)= 𝑇( 𝑘)+k -----(1)
2
𝑛
Now at the last we get only single element so 𝑘 =1
2
=> 𝑛 = 2𝑘
=> 𝑘 = log⁡
(𝑛)
𝑛 2𝑘
Now from equation 1 T(n)=𝑇 +𝑘 =𝑇 + log 𝑛 = 𝑇 1 + log 𝑛 ≡ 𝑂 𝑙𝑜𝑔𝑛
2𝑘 2𝑘
4. Interpolation Search
Interpolation search, also called as extrapolation search. Interpolation
searching algorithm is only used when the elements in an array is sorted and
evenly distributed. The Interpolation Search is an improvement over Binary
Search for instances, where the values in a sorted array are uniformly distributed.
Binary Search always goes to the middle element to check. On the other hand,
interpolation search may go to different locations according to the value of the key
being searched. For example, if the value of the key is closer to the last element,
interpolation search is likely to start search toward the end side.
To find the position to be searched, it uses following formula.
1,6,11,16,21,26
01 2 3 4 5
X=6
L=0
HI=5
A[L]=1
A[H]=26
POS

// The idea of formula is to return higher value of pos


// when element to be searched is closer to arr[hi]. And
// smaller value when closer to arr[lo]
pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]

arr[] ==> Array where elements need to be searched


x ==> Element to be searched
lo ==> Starting index in arr[]
hi ==> Ending index in arr[]
Algorithm
Rest of the Interpolation algorithm is same except the above partition logic.
Step1: In a loop, calculate the value of “pos” using the probe position formula.
Step2: If it is a match, return the index of the item, and exit.
Step3: If the item is less than arr[pos], calculate the probe position of the left sub-
array. Otherwise calculate the same in the right sub-array.
Step4: Repeat until a match is found or the sub-array reduces to zero.

Advantages

When all elements in the list are sorted and evenly distributed, then executing time
of Interpolation search algorithm is log(log n) i.e) Best case.

Disadvantages

 However, When the elements in the list are increased exponentially, then
executing time of Interpolation search algorithm is 0(n) i.e Worst case.
 Not applicable for linked list data structure

Pseudo code
A → Array list
N → Size of A
X → Target Value
Procedure Interpolation_Search()
Set lo → 0
Set Mid → -1
Set Hi → N-1
While Lo<=Hi and x>=a[Lo] and x<=a[Hi]

Set Mid = Lo + ((Hi - Lo) * (X - A[Lo])/ (A[Hi] - A[Lo]))


if A[Mid] = X
EXIT: Success, Target found at Mid
else
if A[Mid] < X
Set Lo to Mid+1
else if A[Mid] > X
Set Hi to Mid-1
end if
end if
End While
End Procedure

Time Complexity
Best Case Avg Case Worst Case
O(log(log n)) O(log(log n)) O(n)

Best case : if all elements of the list are uniformly distributed


Worst case: if elements of the list are not uniformly distributed

5 7 9 10 10 11 12 13
0 1 2 3 4 5 6 7 8

pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]


x=6
l=1,h=7,a[1]=7,a[7]=13
pos=1+[(6-7)*(7-1)]/(13-7)=0

a[pos]==x
a[pos]<x
l=pos+1
a[pos]>x
h=pos-1

You might also like