Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
248 views

Linear Search in Java

Linear search is a simple algorithm that searches for a target value in a list by checking each element in sequence until it is found or the whole list has been searched. It has a linear time complexity of O(n). Binary search is more efficient for sorted lists, with a time complexity of O(log n). It works by comparing the target to the middle element and then searching only the half of the list in which the target could be based on the comparison.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
248 views

Linear Search in Java

Linear search is a simple algorithm that searches for a target value in a list by checking each element in sequence until it is found or the whole list has been searched. It has a linear time complexity of O(n). Binary search is more efficient for sorted lists, with a time complexity of O(log n). It works by comparing the target to the middle element and then searching only the half of the list in which the target could be based on the comparison.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Linear Search in Java

Linear search is used to search a key element from multiple elements. Linear search is less used today
because it is slower than binary search and hashing.

Algorithm:

Step 1: Traverse the array

Step 2: Match the key element with array element

Step 3: If key element is found, return the index position of the array element

Step 4: If key element is not found, return -1

Linear search is a very simple search algorithm. In this type of search, a sequential search is made over
all items one by one. Every item is checked and if a match is found then that particular item is returned,
otherwise the search continues till the end of the data collection.

Linear Search

Linear search is a very basic and simple search algorithm. In Linear search, we search an element or
value in a given array by traversing the array from the starting, till the desired element or value is found.

It compares the element to be searched with all the elements present in the array and when the
element is matched successfully, it returns the index of the element in the array, else it return -1.

Linear Search is applied on unsorted or unordered lists, when there are fewer elements in a list.

Features of Linear Search Algorithm

It is used for unsorted and unordered small list of elements.

It has a time complexity of O (n), which means the time is linearly dependent on the number of
elements, which is not bad, but not that good too.

It has a very simple implementation.

We will implement the Linear Search algorithm in the next tutorial.

Binary Search

Binary Search is used with sorted array or list. In binary search, we follow the following steps:

We start by comparing the element to be searched with the element in the middle of the list/array.
If we get a match, we return the index of the middle element.

If we do not get a match, we check whether the element to be searched is less or greater than in value
than the middle element.

If the element/number to be searched is greater in value than the middle number, then we pick the
elements on the right side of the middle element(as the list/array is sorted, hence on the right, we will
have all the numbers greater than the middle number), and start again from the step 1.

If the element/number to be searched is lesser in value than the middle number, then we pick the
elements on the left side of the middle element, and start again from the step 1.

Binary Search is useful when there are large number of elements in an array and they are sorted.

So a necessary condition for Binary search to work is that the list/array should be sorted.

Features of Binary Search

It is great to search through large sorted arrays.

It has a time complexity of O(log n) which is a very good time complexity. We will discuss this in details
in the Binary Search tutorial.

It has a simple implementation.

Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm
works on the principle of divide and conquer. For this algorithm to work properly, the data collection
should be in the sorted form.

Binary search looks for a particular item by comparing the middle most item of the collection. If a match
occurs, then the index of item is returned. If the middle item is greater than the item, then the item is
searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-
array to the right of the middle item. This process continues on the sub-array as well until the size of the
subarray reduces to zero.

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.

You might also like