☞We can perform following basic operations on an array.
1. Searching ➺ To search any element in an array. 2. Sorting ➺ To arrange elements in ascending or descending order
☞To perform search operation, we will use following techniques :
1. Linear Search 2. Binary Search
☞It is also known as sequential search.
☞In this search technique, we start at the beginning of a list and search for the desired element by checking each subsequent element until either the desired element is found or list is exhausted.
import java.util.*;
class LinearSearch{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size");
int size=sc.nextInt();
int arr[]=new int[size];
int i, numSearch, flag=1;
System.out.println("Enter the elements");
for(i=0;i<arr.length;i++)
arr[i]=sc.nextInt();
System.out.println("Enter the number to be searched");
numSearch=sc.nextInt();
for(i=0;i<arr.length;i++){
if(arr[i]==numSearch)
flag=0;
}
if(flag==0)
System.out.println("The number is present");
else
System.out.println("The number is not present");
}
}
Enter the size 5 Enter the elements 87 39 873 76 89 Enter the number to be searched 76 Ther number is present