Find
Search refers to the process of locating whether a specific element exists in a set of data and its exact position. It is a fundamental operation involved in almost every program.
Comparison of Three Search Algorithms
Search Algorithm Complexity Comparison - Number of Operations at n=1000
Summary of Three Search Algorithm Comparisons
Linear Search O(n)
No ordering required
Compare one by one, simple and reliable
Suitable for: small or unordered data
Binary Search O(log n)
Requires ordering
Halve and exclude half each time
Suitable for: general approach for ordered data
Interpolation Search O(log log n)
Requires ordering + uniform distribution
Estimate position by value proportion
Suitable for: optimal when uniformly distributed
Linear search
Start from the first element and compare one by one until the target is found or traversal is complete. No ordering required; time complexity O(n).
Binary Search
Requires the data to be already sorted. Each time it compares with the middle element and halves the search range; time complexity O(log n).
Interpolation Search
An optimization over binary search. Instead of taking the midpoint, it estimates the likely position based on the target value. Achieves O(log log n) when the data is uniformly distributed.
Example
#include
/* Linear search: O(n), no ordering required */
int
linearSearch
(
int
arr
[
]
,
int
n
,
int
target
)
{
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
if
(
arr
[
i
]
==
target
)
return
i
;
}
return
1
;
}
/* Binary search: O(log n), requires a sorted (ascending) array */
int
binarySearch
(
int
arr
[
]
,
int
left
,
int
right
,
int
target
)
{
while
(
left
<=
right
)
{
int
mid
=
left
(
right
left
)
/
2
;
/* Overflow-safe writing */
if
(
arr
[
mid
]
==
target
)
return
mid
;
/* Found */
if
(
arr
[
mid
]
<
target
)
left
=
mid
1;
/* Target is in the right half */
else
right
=
mid
1
;
/* Target is in the left half */
}
return
1
;
/* Not found */
}
/* Interpolation search: O(log log n) average, requires uniform data distribution */
int
interpolationSearch
(
int
arr
[
]
,
int
n
,
int
target
)
{
int
low
=
0
,
high
=
n
1
;
while
(
low
<=
high
&&
target
=
arr
[
low
]
&&
target
<=
arr
[
high
]
)
{
/* Interpolation formula: estimate position based on the value's proportion */
int
pos
=
low
(
(
target
arr
[
low
]
)
(
high
low
)
)
/
(
arr
[
high
]
arr
[
low
]
)
;
if
(
arr
[
pos
]
==
target
)
return
pos
;
if
(
arr
[
pos
]
<
target
)
low
=
pos
1;
else
high
=
pos
1
;
}
return
1
;
}
int
main
(
)
{
int
arr
[
]
=
{
10
,
20
,
30
,
40
,
50
,
60
,
70
,
80
,
90
}
;
int
n
=
9
;
printf
(
"Linear search 50: index %d\n"
,
linearSearch
(
arr
,
n
,
50
)
)
;
/* Output: 4 */
printf
(
"Binary search 50: index %d\n"
,
binarySearch
(
arr
,
0
,
n
1
,
50
)
)
;
/* Output: 4 */
printf
(
"Interpolation search 50: index %d\n"
,
interpolationSearch
(
arr
,
n
,
50
)
)
;
/* Output: 4 */
return
0
;
}
Complexity Comparison Summary
| Algorithm | Time Complexity | Requires ordering | Applicable scenarios |
|---|---|---|---|
| Linear search | O(n) | No | Small or unordered data |
| Binary Search | O(log n) | Is | Ordered array, generally efficient |
| Interpolation Search | O(log log n) average | Is | Specific scenarios with uniformly distributed data |