Header Ads

Define a class to declare an integer array of size n and accept the elements into the array. Search for an element input by the user using linear search technique, display the element if it is found, otherwise display the message “NO SUCH ELEMENT.

What is array ?

Array, in the Java context, are a variable object that serves as a container to hold a fixed number of values ​​of the same type. By declaring the array , the memory space is assigned to values ​​of a certain type. At the time of creation, the lengths of the Array should be specified and remain the same.
To access the element of the same members, the numerical indicator (non-negative value) corresponding to the location of the item must be used. The first indicator value in the list is zero, hence, the index with the fourth value is used to reach the fifth element in the array.


Linear Search Technique :



Linear search is used to search for a element form many elements in array. Linear search is rarely 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 the key element is not found, return -1



Code :


import java.util.Scanner;

public class search {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Array Size : ");
int n = sc.nextInt();

int arr[] = new int[n];
System.out.println("Enter "+n+" numbers:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

System.out.println("Enter the element to search :");
int s = sc.nextInt();

int j;
for (j = 0; j < n; j++) {
if (arr[j] == s) {
System.out.println(s + " found at index " + j + " and position " + (j+1));
break;
}
}
if (j == n) {
System.out.println("NO SUCH ELEMENT");
}
}
}


Output :

  1. When the number is found in the array
Enter Array Size : 
5
Enter 5 numbers:
1
2
3
4
5
Enter the element to search :
3
3 found at index 2 and position 3

    2. When the number is not found in the array

Enter Array Size : 
5
Enter 5 numbers:
6
7
8
9
10
Enter the element to search :
3
NO SUCH ELEMENT

Explanation :

After importing 'java.util.scanner' create a scanner object which is used to take values from the user. Take the size of array from the user and store it in variable 'n'. Declare an array with size n. Insert the values into the array using for loop. First phase of the program is completed here. 
Now we have to search the element in the array. Give a prompt to the user to input the element to search and store it in a variable lets say 's'. Declare of a variable 'j' of integer type. Now traverse the array using for loop and compare the value of index 'j' in the array with 's'. If both are equal then print 's + " found at index " + j + " and position " + (j+1)' and use break statement to get out of the loop. If the whole array is iterated the value of j will be equal to n which means element is not found in the array so the 2nd if statement will be true and NO SUCH ELEMENT is printed.

You will get a logical error of you use if else block here. It will print NO SUCH ELEMENT for every iteration if the element is not found.
For Example : If the element is found at 2nd index then the output will be
Enter Array Size : 
5
Enter 5 numbers:
1
2
3
4
5
Enter the element to search :
3
NO SUCH ELEMENT
NO SUCH ELEMENT
3 found at index 2 and position 3

Post a Comment

0 Comments