Linear search is a simple searching algorithm that sequentially checks each element in a list or array until a match is found or the end of the list is reached. It is called "linear" because it scans the elements in a linear manner, one by one. It is useful when the list or array is not sorted. If the list or array is sorted then go for binary search.
click here to learn binary search algorithm in Java
Here's how the linear search algorithm works:
Start at the beginning of the list or array.
Compare the target element (the element you are searching for) with the current element in the list.
If the current element matches the target element, the search is successful, and you can return the index of the current element.
If the current element does not match the target element, move to the next element in the list.
Repeat steps 2-4 until a match is found or the end of the list is reached.
If the end of the list is reached without finding a match, the search is unsuccessful, and you can return a special value (e.g., -1) to indicate that the target element is not present in the list.
Let's consider an example to explain the linear search algorithm:
Suppose you have a list of numbers: [8, 2, 13, 7, 5]. We want to find the index of the number 7 in this list using linear search.
Start at the beginning of the list. We begin with the first element, which is 8.
Compare the current element (8) with the target element (7). Since they don't match, we move to the next element.
Move to the next element in the list, which is 2.
Compare the current element (2) with the target element (7). Again, they don't match, so we move to the next element.
Move to the next element in the list, which is 13.
Compare the current element (13) with the target element (7). They don't match, so we continue to the next element.
Move to the next element in the list, which is 7.
Compare the current element (7) with the target element (7). Hurray! We have found a match!
We can now return the index of the element 7, which is 3 (assuming 0-based indexing).
Code in java :
public class Search {
private static int index;
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,6,7,12,13,14,15,15,15,18,19};
int numberToFind=19;
index = -1;
search(arr, 0, arr.length, numberToFind);
System.out.println("index of " +numberToFind+ " is : " + index);
}
private static void search(int[] arr, int start, int end, int numberToFind) {
//Linear search algorithm starts
for (int i = start; i < end; i++) {
if(arr[i] == numberToFind){
index = i;
break;
}
}
}
}
The linear search algorithm is straight forward and easy to implement, but it is not the most efficient for large lists. Its time complexity is O(n), where n is the number of elements in the list. This means that the time it takes to search for an element grows linearly with the size of the list.
output:
index of 19 is : 15
BONUS CODE
If we have a large number of elements in an array then the time of search increases, to reduce this time can we use threads?
of course, we can. But remember one thing, creating threads is a very expensive task for the CPU so if your array isn't big enough then it will be a disadvantage, hence use threads for linear search only if you are sure that the array is going to be big.
How to do it?
well, it's easy, create one thread that will search the first half of the array and create another thread that will search the other half of the array.
Let's take an example. suppose we have an array [8, 2, 13, 7, 5, 12, 4, 5] and we are looking for the number 12.
here the length of the array is 8, so the mid index is 8/2 i.e 4
Now we will create a thread that will do a linear search on the 0th to 3rd index.
Then we will create another thread that will do a linear search on the 4th to 7th index.
Then we will start both the thread and both the threads can search parallelly.
As per our example, the target number 12 is in the 5th index. The second thread will set the index as 5 and the moment we get the index we will stop both the searching operation going on by threads.
Now the tough part, writing code to create threads and doing tasks is very complicated. But don't worry. ๐๐
public class Search {
private static int index;
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,6,7,12,13,14,15,15,15,18,19};
int numberToFind=19;
index = -1;
linearSearch(arr, numberToFind);
System.out.println("index of " +numberToFind+ " is : " + index);
}
public static int linearSearch(int [] arr, int numberToFind){
int mid = arr.length/2;
Thread leftThread = new Thread(()-> search(arr, 0, mid, numberToFind ));
Thread rightThread = new Thread(()-> search(arr, mid, arr.length, numberToFind));
leftThread.start();
rightThread.start();
try {
//join both threads so the main thread will execute once these two are done
leftThread.join();
rightThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
return index;
}
private static void search(int[] arr, int start, int end, int numberToFind) {
for (int i = start; i < end; i++) {
if(index != -1)
break;
if(arr[i] == numberToFind){
index = i;
break;
}
}
}
}
output:
index of 19 is : 15