Linear search is a basic search algorithm in Java that is used to find a specific element in an array. It works by iterating through each element in the array one by one, comparing the target value to each element in the array until a match is found or the entire array is searched. Sequential search is also known as linear search.
Problem Description
Write a java program to find the minimum element of the sequence using the technique of linear search.
Problem Solution
To find the minimum element in a sequence, we can use linear search. We begin by initializing the min variable to the first element of the sequence. We then traverse each element of the sequence and compare it to the current value of min. If the element is smaller than min, we update min to the new value.
Program/Source Code
Here is the source code of the Java Program to Find Minimum Element in an Array using Linear Search. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a java program to find the minimum element using Sequential Search
import java.util.Random;
import java.util.Scanner;
public class Minimum_Using_Sequential
{
static int N = 20;
static int []sequence = new int[N];
public static int minSequential()
{
int min = sequence[0];
for(int i=0; i<N; i++)
if(min > sequence[i])
min = sequence[i];
return min;
}
public static void main(String args[])
{
Random random = new Random();
for(int i=0; i<N; i++)
sequence[i] = Math.abs(random.nextInt(100));
System.out.println("The sequence is :");
for(int i=0; i<N; i++)
System.out.print(sequence[i] + " ");
Scanner sc = new Scanner(System.in);
System.out.println("\nThe minimum element in the sequence is : " + minSequential());
sc.close();
}
}
Program Explanation
1. This is a Java program that generates a sequence of 20 random integers between 0 and 100, finds the minimum element in the sequence using sequential search, and prints the minimum element to the console.
2. The program imports the Random and Scanner classes from the java.util package.
3. A class called Minimum_Using_Sequential is defined.
4. An integer variable N and an integer array sequence of size N (set to 20) are declared as static variables in the class.
5. A static method called minSequential is defined in the class which returns the minimum element of the sequence array using sequential search.
6. The main method is defined in the class, which generates a sequence of 20 random integers between 0 and 100 using the Random class and the Math.abs() method.
7. The minSequential method is called to find the minimum element in the sequence using sequential search.
8. Finally, the minimum element is printed.
Program Output
In this case the program generates a sequence of 20 random integers and finds the minimum element in the sequence using linear/sequential search.
$ javac Minimum_Using_Sequential.java
$ java Minimum_Using_Sequential
The sequence is :
33 43 61 93 97 31 53 62 58 87 68 61 16 52 12 29 27 63 68 22
The minimum element in the sequence is : 12
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.