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

Linear and Binary Search

The document discusses linear and binary search algorithms. Linear search sequentially checks each element of an unsorted array to find a target value. It has O(n) time complexity but requires no preprocessing. Binary search works on a sorted array by dividing the search space in half at each step. It has O(log n) time complexity but requires the array to be sorted first. Both algorithms require O(1) space.

Uploaded by

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

Linear and Binary Search

The document discusses linear and binary search algorithms. Linear search sequentially checks each element of an unsorted array to find a target value. It has O(n) time complexity but requires no preprocessing. Binary search works on a sorted array by dividing the search space in half at each step. It has O(log n) time complexity but requires the array to be sorted first. Both algorithms require O(1) space.

Uploaded by

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

Understanding the Linear Search Algorithm

How can we search an element in an array?

In linear search, we adopt the most naive method for searching.

 We look through the array in an orderly fashion, comparing each element with the element we would
like to find, namely the query element.
 Whenever we find a matching element, we report the index of the matched element
 If we have traversed the entire array and not found a matching element, we report that the element is
not present in the array.

When should we stop?

Important Observations

Let's take note of a few important observations:

 We do not require any ordering of the elements of the array to find a particular element in the array.
 This is because we iterate through the entire array for searching the element, so we are guaranteed to
find the element if it present in the array.
 If the element is not present in the array we are guaranteed that the algorithm returns failure.
Step by Step Process for Searching an Element

Linear Search Algorithm

Let's have a final look at the consolidated algorithm to search for an element in an array of N elements:

 STEP 1: Start from the leftmost element of list and one by one compare the query element with each
element of the list.
 STEP 2: If the query element matches with an element, return the index.
 STEP 3: If the query element doesn’t match with any of elements, return failure.

Understanding the Binary Search Algorithm


Idea

The idea of binary search is to use the information that the array is sorted and reduce the time complexity to
O(Log n). In Binary search, we adopt the following strategy.

 Sort the array elements.


 Search a sorted array by repeatedly dividing the search interval in half.
 Begin with an interval covering the whole array.
 If the value of the search key is less than the item in the middle of the interval, narrow the interval to
the lower half.
 Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

Important Observations

Let's take note of a few important observations:

 It is crucial for the element array to be sorted to be able to search for a query element.
 We jump intervals and prune out the array intervals that we are sure will not contain the query element.
element
 If the element is not present
ent in the array we are guaranteed that the algorithm returns failure.
Step by Step Process for Binary Searching an Element

Binary Search Algorithm

Let's have a final look at the consolidated algorithm to search for an element in an array of N elements:

 STEP 1: Start with the middle element.


o If the target value is equal to the middle element of the array, then return the index of the
middle element.
o If not, then compare the middle element with the target value,
 If the target value is greater than the number in the middle index, then pick the
elements to the right of the middle index, and start with Step 1.
 If the target value is less than the number in the middle index, then pick the elements
to the left of the middle index, and start with Step 1.
 STEP 2: When a match is found, return the index of the element matched.
 STEP 3: If no match is found, then return failure.

Running Time of Linear Search

A linear search scans one item at a time, without jumping to any item.

 The worst case complexity is O(n), ssometimes known an O(n) search


 Time taken to search elements keeps increasing as the number of elements is increased.
Running Time of Binary Search

A binary search cuts down your search to half as soon as you find middle of a sorted list.

 The middle element is looked to check if it is greater than or less than the value to be searched.
 Accordingly, search is done to either half of the given list.
 The worst case time complexity is O(logn).

Space Complexity of Linear Search

No auxiliary space is required in linear search. The elements of the array are directly compared with the query
element to find a match. Thus the space complexity for the same is O(1).

Space Complexity of Iterative Binary Search

You need to maintain a start, end and middle variable to know the window of elements you are checking. Thus
constant space is required to perform iterative binary search. Thus the space complexity for the same is O(1).

Linear Search

Linear search is used on collections of items. It relies on the technique of traversing a list from start to end by
exploring properties of all the elements that are found on the way.

For example, consider an array of integers of size N. You should find and print the position of all the elements
with value x. Here, the linear search is based on the idea of matching each element from the beginning of the
list to the end of the list with the integer x, and then printing the position of the element if the condition is `True'.

Implementation:

The pseudo code for this example is as follows:

for(start to end of array)


{
if (current_element equals to 5)
{
print (current_index);
}
}

Binary Search

Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].
A simple approach is to do search. The time complexity of above algorithm is O(n). Another approach to
perform the same task is using Binary Search.

Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval
covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow
the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or
the interval is empty.
Binary search is the most popular Search algorithm. It is efficient and also one of the most commonly used
techniques that is used to solve problems.

If all the names in the world are written down together in order and you want to search for the position of a
specific name, binary search will accomplish this in a maximum of 35 iterations.

Binary search works only on a sorted set of elements. To use binary search on a collection, the collection must
first be sorted.

When binary search is used to perform operations on a sorted set, the number of iterations can always be
reduced on the basis of the value that is being searched.

The idea of binary search is to use the information that the array is sorted and reduce the time complexity to
O(Log n).
We basically ignore half of the elements just after one comparison.
1. Compare x with the middle element.
2. If x matches with middle element, we return the mid index.
3. Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element.
So we recur for right half.
4. Else (x is smaller) recur for the left half.
Recursive implementation of Binary Search

/ C++ program to implement recursive Binary Search


#include <bits/stdc++.h>
using namespace std;
// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;

// If the element is present at the middle


// itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);

// Else the element can only be present


// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
Output :
Element is present at index 3
Iterative implementation of Binary Search

// C++ program to implement iterative Binary Search


#include <bits/stdc++.h>
using namespace std;

// An iterative binary search function. It returns


// location of x in given array arr[l..r] if present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;

// Check if x is present at mid


if (arr[m] == x)
return m;

// If x greater, ignore left half


if (arr[m] < x)
l = m + 1;

// If x is smaller, ignore right half


else
r = m - 1;
}

// if we reach here, then element was


// not present
return -1;
}

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
Output :
Element is present at index 3

You might also like