Java Program to Find Minimum Element in an Array using Linear Search

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.

  1. //This is a java program to find the minimum element using Sequential Search
  2.  
  3.  
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. public class Minimum_Using_Sequential 
  8. {
  9.     static int N = 20;
  10.     static int []sequence = new int[N];
  11.  
  12.     public static int minSequential()
  13.     {
  14.         int min = sequence[0];
  15.         for(int i=0; i<N; i++)
  16.             if(min > sequence[i])
  17.                 min = sequence[i];
  18.  
  19.         return min;
  20.     }
  21.     public static void main(String args[])
  22.     {
  23.         Random random = new Random();
  24.  
  25.         for(int i=0; i<N; i++)
  26.             sequence[i] = Math.abs(random.nextInt(100));
  27.         System.out.println("The sequence is :");
  28.         for(int i=0; i<N; i++)
  29.             System.out.print(sequence[i] + " ");     
  30.  
  31.         Scanner sc = new Scanner(System.in);
  32.         System.out.println("\nThe minimum element in the sequence is : " + minSequential());
  33.         sc.close();
  34.     }
  35. }
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.

advertisement
advertisement
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.

Note: Join free Sanfoundry classes at Telegram or Youtube

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.