0% found this document useful (0 votes)
69 views4 pages

Linear and Binary Search

There are two approaches to searching arrays: linear/sequential search and binary search. Linear search has a time complexity of O(n) as it sequentially checks each element. Binary search has a time complexity of O(log n) as it repeatedly halves the search space by comparing the target value to the middle element of the sorted array. Key differences are that binary search requires a sorted array and has faster access while linear search can be used on unsorted arrays but is slower.

Uploaded by

Xee- bii
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
69 views4 pages

Linear and Binary Search

There are two approaches to searching arrays: linear/sequential search and binary search. Linear search has a time complexity of O(n) as it sequentially checks each element. Binary search has a time complexity of O(log n) as it repeatedly halves the search space by comparing the target value to the middle element of the sorted array. Key differences are that binary search requires a sorted array and has faster access while linear search can be used on unsorted arrays but is slower.

Uploaded by

Xee- bii
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

Searching in Arrays

When it comes to search arrays there are two approaches exist 1) un-sorted array search
using Sequential/linear search.

2) Sorted array search using binary search.

If 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 a linear search. The time complexity of the 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. Example :

Algorithm:

Here low contains the starting index of array and high contains the last index and mid contain the middle index of
array

1. Set low=0 and high=n-1;


2. Repeat step 1 to 3 until low<=high
3. Compare x with the middle element.
4. If x matches with the middle element, we return the mid index.
5. Else If x is greater than the mid element, then set low = mid+1;
6. exit
7. Else (x is smaller) the set high=mid-1;
Binary search

package binarysearch; public static void main(String args[])


{
/* // Create object of this class
* @author Xee-biii
*/
Binarysearch ob = new Binarysearch();
public class Binarysearch
// Given array arr[]
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int binarySearch(int arr[], int l, int r, int x )
int x = 6;
{
if (r >= l)
// Function Call
{ int result = ob.binarySearch(arr, 0 , n - 1, x);
int mid = l + (r - l) / 2;
// If the element is present
// at the middle itself if (result == -1)
if (arr[mid] == x) System.out.println("Element " + "not present");
return mid; else
// If element is smaller than System.out.println("Element found" + " at index
// mid, then it can only be
// present in left subarray " + result);
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x); } //main end
// Else the element can only be
// present in right subarray
} // class end
return binarySearch(arr, mid + 1, r, x);
}
// Reach here when element is
// not present in array
return -1;
}

Linear Search:
Linear search is one of the simplest approach when it comes to searching in arrays

Algorithm:
1. Start from the leftmost element of arr[] and one by one compare x with each element of arr[]

2. If x matches with an element, return the index.


3. If x doesn’t match with any of elements, return -1.
Implementation:

public class Sequentialsearch


{
public static int sequentialSearch(int arr[], int x)
{
int n = arr.length;
for (int i = 0; i < n; i++)
{
if (arr[i] == x)
return i;
}
return -1;
}
public static void main(String[] args)
{

int arr[] = { 2, 3, 4, 10, 40 };


int x = 10;
Output
Element is present at
// Function call index 3
int result = sequentialSearch(arr, x);
if (result == -1)
System.out.print(
"Element is not present in array");
else
System.out.print("Element is present at index "
+ result);
}
}
Linear Search vs Binary Search
Important Differences
 Input data needs to be sorted in Binary Search and not in Linear Search
 Linear search does the sequential access whereas Binary search access data
randomly.
 Time complexity of linear search -O(n) , Binary search has time complexity O(log n).
  Linear search performs equality comparisons and Binary search performs ordering
comparisons

 Linear

Binary

You might also like