A Comparative Analysis of Three Differen
A Comparative Analysis of Three Differen
Abstract: Searching is a process of checking and finding an element from a list of elements. Although there are huge
numbers of searching algorithms are available. But here our work intends to show an overview of comparison between
three different types of searching algorithms. We have tried to cover some technical aspects of Linear or Sequential
search, Binary Search and Interpolation Search. This research provides a detailed study of how all the three algorithms
work & give their performance analysis with respect to time complexity.
Keywords: Complexity, Linear Search, Binary Search, Interpolation search, time complexity
I. INTRODUCTION
In the present scenario an algorithm and data structure concept of three searching techniques with examples,
play a significant role for the implementation and design algorithm of each searching process, time complexity have
of any software [1]. An algorithm is a finite sequential set discussed here. Finally, conclusions were presented in
of instructions which, if followed, accomplish a particular section 5.
task or a set of tasks in a finite time.
II. CONCEPT BEHIND SEARCHING
Complexity: The complexity of an algorithm is a function PROCESS
g(n) that gives the upper bound of the number of In linear search, each element of an array is read one by
operations performed by an algorithm when the input size one sequentially and it is compared with the desired
is n. Complexity are divided in two ways. Time complexity element. A search will be unsuccessful if all the elements
is the amount of time the computer requires to execute the are read and the desired element is not found. Where
algorithm& Space complexity of an algorithm is the Binary search is an extremely efficient algorithm when it
amount of memory space the computer requires, is compared to linear search. Binary search technique
completing the execution of the algorithm. In case of searches ―data‖ in minimum possible comparisons.
algorithm searching is the process to finding to location of Suppose the given array is a sorted one, otherwise first we
the given data elements in the data structure. The different have to sort the array elements. Then apply the following
types of searching techniques are Linear search, Binary conditions to search a ―data‖.
search.
1) Find the middle element of the array (i.e., n/ 2 is the
Linear (Sequential) Search is the basic and simple method middle element if the array or the sub-array contains n
of searching: It is a method where the search begins at the elements).
end of the list, scans the elements of the list from left to 2) Compare the middle element with the data to be
right until the desired record is found. searched and then there are following three cases.
In Binary search the entire sorted list is divided into two a) If it is a desired element, then search is successful.
parts. We first compare our input item with the mid b) If it is less than desired data, then search only the first
element of the list & then restrict our attention to only the half of the array, i.e., the elements which come to the
first or second half of the list depending on whether the left side of the middle element.
input item comes left or right of the mid element. In this c) If it is greater than the desired data, then search only
way we reduce the length of the list to be searched by half. the second half of the array, i.e., the elements which
Less time is taken by binary search to search an element come to the right side of the middle element.
from the sorted list of elements .So we can conclude that
binary search method is more efficient than the linear Repeat the same steps until an element are found or
search. Binary search algorithm is efficient because it is exhaust the search area. Again For searching an ordered
based on divide-and-conquer strategy; which divides the array, Interpolation search is used. This method is even
list into two parts and searches one part of the list therebymore efficient than binary search, if the elements are
reducing the search time [2]. uniformly distributed (or sorted) in an array A.
Interpolation search is a method of retrieving a desired
Interpolation Search (sometimes referred to record by key in an ordered file by using the value of the
as extrapolation search) is an algorithm for searching for a key and the statistical distribution of the Keys [3].
given key value in an indexed array that has
been ordered by the values of the key. In this paper, Consider an array A of n elements and the elements are
mainly we have reviewed three searching algorithms .The uniformly distributed (or the elements are arranged in a
sorted array). Initially, as in binary search, low is set to 0
and high is set to n – 1. Now we are searching an element Since (A [4] < data) - i.e., 40 < 45 - reinitialise
key in an array between A[low] and A[high]. the variable LB, UB and mid
⇒key< A[2] ⇒ 34 < 35 so reset 3) Repeat the steps 4 through 7 until if(low < high)
high = mid–1 ⇒ 1 4) Mid = low + (high – low) × ((key – A[low]) /
low = 0 (A[high] – A[low]))
high = 1 5) If(key < A[mid])
Since (low < high) high = mid–1
mid = 0+(1–0) × ((34–2)/ (25–2)) 6) Elseif (key > A[mid]) low = mid + 1
= 3 × (32/ 23) 7) ElseDisplay ― The key is not in the array‖
= 1 here (key > A[mid]) 8) STOP
⇒key> A[1] ⇒ 34 > 25 V. OBSERVATIONS
so reset low = mid+1 A. Linear or Sequential Search :
⇒2 Time Complexity of the linear search is found by number
low = 2 of comparisons made in searching a record.
high = 1
Since (low > high) Suppose there are n elements in the list. The following
So― The key is not in the array‖ expression, gives average number of comparisons
„ 1+2+ …..+n / n
IV. ALGORITHM „ 1+2+ …..+n = n(n+1)/2
A. Linear or Sequential Search : „ Thus the following expression gives the average number
Let A be an array of n elements, A[1],A[2],A[3], ...... of comparisons made by sequential search in successful
A[n]. ―data‖ is the element to be searched. Then this case :
algorithm will find the location ―loc‖ of data in A. Set loc
= – 1,if the search is unsuccessful. 1+2+……+n=1n(n+1) = n+1
n 2 2
1. Input an array A of n elements and ―data‖ to be In the best case, the desired element is present in the first
searched and initialise loc = – 1. position of the array, i.e., only one comparison is made. So
2. Initialise i = 0; and repeat through step 3 if (i<n) by f (n) = O(1). In the Average case, the desired element is
incrementing i by one . found in the half position of the array, then f (n) = O[(n +
3. If (data = A[i]) (a) loc = i 1)/ 2]. But in the worst case the desired element is present
4. (b) GOTO step 4 in the nth (or last) position of the array, so n comparisons
5. If (loc> 0) are made. So f (n) = O(n + 1)
a. Display ―data is found and searching is successful‖
6. Else B. Binary Search :
a. Display ―data is not found and searching is Time Complexity is measured by the number f (n) of
unsuccessful‖ comparisons to locate ―data‖ in A, which contain n
7. Exit elements. Each step of the algorithm divides the block of
items being searched in half. We can divide a set
B. Binary Search : of n items in half at most log2 n times. Thus the running
Let A be an array of n elements A [1], A [2], A time of a binary search is proportional to log n and we say
[3],......A[n]. ―Data‖ is an element to be searched. ―mid‖ this is a O(log n) algorithm. Observe that in each
denotes the middle location of a segment (or array or sub- comparison the size of the search area is reduced by half.
array) of the element of A. LB and UB is the lower and Hence in the worst case, at most log2n comparisons
upper bound of the array which is under consideration. required. So f (n) = O([log2n ]+1). Time Complexity in the
average case is almost approximately equal to the running
1. Input an array A of n elements and ―data‖ to be sorted time of the worst case. Binary search requires a more
2. LB = 0, UB = n; mid = int ((LB+UB)/ 2) complex program than our original search and thus
3. Repeat step 4 and 5 while (LB <= UB) and (A[mid]! for small n it may run slower than the simple linear search.
= data) However, for large n,
4. If (data < A[mid]) UB = mid–1
5. Else LB = mid + 1
6. Mid = int ((LB + UB)/ 2)
7. If (A[mid] == data) Display ―the data found‖
8. Else Display ―the data is not found‖
9. Exit
C. Interpolation Search :
Suppose A be array of sorted elements and key is the
elements to be searched and low represents the lower
bound of the array and high represents higher bound of the
array.
1) Input a sorted array of n elements and the key to Fig. 1. : Pictorial representation of Binary Search
be searched Technique
2) Initialise low = 0 and high = n – 1
VII. CONCLUSION
Searching is very important for huge application such as
database management system. In this work, we have tried
to summarize three searching algorithms and also included
searching concept, algorithm, coding & time complexity.
Coding part is implemented in C language. Rather we
have also discussed about the advantages of binary search
algorithm with respect to others for a given problem.
ACKNOWLEDGMENT
The authors would like to thank the authorities of Ghani
Khan Chowdhury Institute of Engineering & Technology,
Malda, West Bengal & Birbhum Institute of Engineering
& Technology, Suri, Birbhum-731101, West Bengal for
providing every kind of supports and encouragement
during the working process.
REFERENCES
[1] Ms.NidhiChhajed Assistant Professor C.S.E Dept. PIES, Mr. Imran
Uddin Assistant Professor, C.S.E Dept. PIES, Mr.Simarjeet Singh
Bhatia Assistant Professor, C.S.E Dept. PIES Indore (M.P), India.
A Comparison Based Analysis of Four Different Types of Sorting
Algorithms in Data Structures with Their Performances.
[2] Asagba P. O, Osaghae E. O. and Ogheneovo, E. E.Department of
Computer Science, University of Port Harcourt, Choba, Port
Harcourt, Rivers State. Is Binary search technique faster than
Linear search technique?
[3] Yehoshua Perl Bar-Ilan University and The Weizmann Institute of
Science,AlonItaiTechnion--Israel Institute of Technology,
HaimAvni The Weizmann Institute of Science. Interpolation Search
A Log LogNSearch.
[4] YAO, A. C., AND YAO, F. F. The complexity of searching an
ordered random table. InP~ocadmgs of the 17th .4 WILLC11
Symposium OH the Fowz&ztzotzs of Computer Science. 1EEE,
NewYork, 1976, pp. 173-175.
[5] PEARL, Y., ITAI, A., ANDAVNI, H. Interpolation search—A log
log N search. Commun. ACM 21, 7, (1978), 550–554
[6] PEARL. Y., AND REINGOLD, E. M. Understanding the
complexity of interpolation search. lnf.Proc. Left, 6.6 ( 1977), 219–
222
BIOGRAPHIES
Debadrita Roy, born in India, obtained her
M.Tech degree from Heritage Institute of
Technology, Kolkata, W.B. Now she is
Trainer of Information Technology
Department in Ghani Khan Chowdhury
Institute of Engineering & Technology,
Malda-732101, West Bengal, India. She is Life member of
International Association of Engineers India.