You are on page 1of 4

UNIT II

Searching Techniques
Searching is the process of looking through the data contained in a data structure and
determining if a specific value is present. (And potentially returning it.) The contains methods of
the ArrayList, for example, is a method that searches the list for a given value and
returns true of false.

1. Linear Search

The most basic search algorithm is a linear search. This is just a fancy name for “start at the first
element and go through the list until you find what you are looking for.” It is the simplest search.
Here is a simple implementation for an array:

public static int linearSearch(int[] a, int value) {


int n = a.length;
for (int i = 0; i < n; i++) {
if (a[i] == value)
return i;
}
return -1;
}
If you look carefully at it, you’ll realize that the efficiency is O(N). Is it possible to do better than
this, though?

2. Binary Search

Binary search is a searching algorithm that is faster than O(N). It has a catch, though: It requires
the array you are searching to already be sorted. If you have a sorted array, then you can search it
as follows:

1. Imagine you are searching for element v.


2. Start at the middle of the array. Is v smaller or larger than the middle element? If
smaller, then do the algorithm again on the left half of the array. If larger, then do the
algorithm again on the right half of the array.

Basically, with every step, we cut the number of elements we are searching for in half. Here is a
simple implementation:

public static int binarySearch(int[] a, int value) {


int lowIndex = 0;
int highIndex = a.length - 1;
while (lowIndex <= highIndex) {
int midIndex = (lowIndex + highIndex) / 2;
if (value < a[midIndex])
highIndex = midIndex - 1;
else if (value > a[midIndex])
lowIndex = midIndex + 1;
else // found it!
return midIndex;
}
return -1; // The value doesn't exist
}
The efficiency of this is more complicated than the things we’ve been analyzing so far. We
aren’t doing N work in the worst case, because even in the worst case we are cutting the array in
half each step. When an algorithm cuts the data in half at each step, that efficiency
is logarithmic. In this case, O(log2 N)). In efficiency, we frequently drop the base 2 from
logarithmic efficiency, so we can just write this as O(log N) This is significantly better than
an O(N) algorithm.
Data Structures
Home / My courses / BCA204A21T / Unit 2 - SEARCHING & SORTING - Binary Search / COMPARISON BETWEEN SEQUENTIAL AND BINARY SEARCH

COMPARISON BETWEEN SEQUENTIAL AND BINARY SEARCH


Comparison: Sequential / Linear Search & Binary Search

Basis of
Linear / Sequential Search Binary Search
comparison

Linear search is an algorithm to find


an element in a list by sequentially Binary search is an algorithm that finds
Description checking the elements of the list the position of a target value within a
until finding the matching element. sorted array.  
    

A binary search cuts down the search to


A linear search scans one item at a
How It Works half as soon as the middle of a sorted list
time without skipping to any item.  
is found.  

Linear searches may be


Binary searches can only be
implemented on any linear
Implementation implemented on data structures where
container (vector, Single Linked list,
two-way traversal is possible.
double linked list).  

Dimensional It can be implemented on both a It can be implemented only on a


array single and multidimensional array. multidimensional array.

Algorithm type Iterative in nature Divide and conquer in nature

Usefulness Easy to use and no need for any Anyhow tricky algorithm and elements
ordered elements. should be organized in order.

Lines of Code Less More

Linear search is easy to use The binary search is a bit complicated


Complexity because there is no need for any with elements being necessarily arranged
ordered elements.   in a given order.  

Size It is preferable for the small-sized It is preferable for the large-size data
data sets. sets.

A binary search requires sorted arrays for


Linear search does not require the
effective performance. This facilitates
Sorted sorted elements hence the
insertion of elements at their required
Elements elements are conveniently inserted
place and in essence maintaining sorted
at the bottom of the list.
lists.

Linear search is repetitive or


iterative as well as uses the Binary search employs divide and
Approach
sequential approach in its conquer approach in its functionality.
functionality.

In linear search, performance is In binary search, performance is done by


Performance
done by equality comparisons. ordering comparisons.

In the linear search, worst case


In the binary search, the worst case
Worse-Case scenario for searching an element
scenario is O(Log2n) number of
Scenario is equivalent to O  number of
similarities.  
comparison.

The best case scenario in a linear


Best-Case The best case scenario is to find the
search is to find the element in the
Scenario element in the middle position O(1).
first position O(1).

Time The time complexity of linear The time complexity of binary search
Complexity search happens to be O is O(log n)
Complexity search happens to be O . is O(log2n).  

Challenge Questions

◄ BINARY SEARCH Jump to... ITERATIVE AND RECURSIVE METHODS ►

You might also like