Computer Applications
Write a program to search for a given ITEM in a given array X[n] using linear search technique. If the ITEM is found, move it at the top of the array. If the ITEM is not found, insert it at the end of the array.
Java
Java Arrays
5 Likes
Answer
import java.util.Scanner;
public class KboatLinearSearch
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter size of array : ");
int n = in.nextInt();
int X[] = new int[n];
int B[];
System.out.println("Enter array elements : ");
for(int i = 0; i < n; i++) {
X[i] = in.nextInt();
}
System.out.println("Enter search item :" );
int item = in.nextInt();
int i = 0;
// linear search
for (i = 0; i < n; i++) {
if (X[i] == item) {
System.out.println("Search item found at index "
+ i);
break;
}
}
// item not found
if (i == n) {
System.out.println("Search item not found");
B = new int[n + 1];
for(int j = 0; j < n; j++) {
B[j] = X[j];
}
B[n] = item;
System.out.println("Array after inserting "
+ item + " at end:");
for(int j = 0; j <= n; j++) {
System.out.print(B[j] + " ");
}
}
// item found
else {
for(int j = i; j >= 1; j--) {
X[j] = X[j - 1];
}
X[0] = item;
System.out.println("Array after moving "
+ item + " to top :");
for(int j = 0; j < n; j++) {
System.out.print(X[j] + " ");
}
}
}
}Variable Description Table
Program Explanation
Output
![BlueJ output of Write a program to search for a given ITEM in a given array X[n] using linear search technique. If the ITEM is found, move it at the top of the array. If the ITEM is not found, insert it at the end of the array. BlueJ output of Write a program to search for a given ITEM in a given array X[n] using linear search technique. If the ITEM is found, move it at the top of the array. If the ITEM is not found, insert it at the end of the array.](https://cdn1.knowledgeboat.com/img/sa10/c7-arrays-p46-1.jpg)
![BlueJ output of Write a program to search for a given ITEM in a given array X[n] using linear search technique. If the ITEM is found, move it at the top of the array. If the ITEM is not found, insert it at the end of the array. BlueJ output of Write a program to search for a given ITEM in a given array X[n] using linear search technique. If the ITEM is found, move it at the top of the array. If the ITEM is not found, insert it at the end of the array.](https://cdn1.knowledgeboat.com/img/sa10/c7-arrays-p46-2.jpg)
Answered By
1 Like
Related Questions
Write a program to search for an ITEM using binary search in array X[10].
The following array of integers is to be arranged in ascending order using the bubble sort technique:
26 21 20 23 29 17
Give the contents of the array at the end of each iteration. Do not write the algorithm.
Write a program to search for 66 and 71 in the following array :
Make use of binary search technique. Also give the intermediate results while executing this algorithm.
Given the following array :
Write a program to sort the above array using exchange selection sort. Give the array status after every iteration.