This is a java program to find the minimum element of the sequence using the technique of linear search. First we assign min equal to the first element of the sequence, then we go on exploring each element of the sequence and compare it with the min element, if less we update min.
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 the technique of 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(); } }
Output:
$ 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
Related posts:
Hướng dẫn Java Design Pattern – Visitor
Spring Boot - Google OAuth2 Sign-In
Database Migrations with Flyway
Spring Boot - Application Properties
A Guide to JUnit 5
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Spring @RequestMapping New Shortcut Annotations
How to Return 404 with Spring WebFlux
Spring Security Registration – Resend Verification Email
Assert an Exception is Thrown in JUnit 4 and 5
Exploring the New Spring Cloud Gateway
Format ZonedDateTime to String
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Spring Boot - Introduction
Jackson – Unmarshall to Collection/Array
Difference Between Wait and Sleep in Java
Jackson – Change Name of Field
JUnit 5 @Test Annotation
Java Program to Implement Iterative Deepening
Custom JUnit 4 Test Runners
Java Program to Implement Bellman-Ford Algorithm
Login For a Spring Web App – Error Handling and Localization
Converting Between an Array and a Set in Java
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Implement Stack
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Default Password Encoder in Spring Security 5
A Guide to the Java LinkedList
Java Program to Implement Gaussian Elimination Algorithm
Java Program to Find Maximum Element in an Array using Binary Search
Spring Security Login Page with React