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:
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Introduction to Using Thymeleaf in Spring
JPA/Hibernate Persistence Context
Java – Write to File
Introduction to Spring Security Expressions
Phương thức tham chiếu trong Java 8 – Method References
Kết hợp Java Reflection và Java Annotations
Spring Data JPA @Modifying Annotation
Java Program to implement Array Deque
Spring Boot - Web Socket
Model, ModelMap, and ModelAndView in Spring MVC
Introduction to the Functional Web Framework in Spring 5
Debug a HttpURLConnection problem
Java – String to Reader
Hướng dẫn Java Design Pattern – Flyweight
Comparing Two HashMaps in Java
How to Iterate Over a Stream With Indices
Spring Security OAuth2 – Simple Token Revocation
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java – File to Reader
Java Program to Implement the RSA Algorithm
Using Optional with Jackson
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Spring Cloud – Securing Services
Getting Started with Forms in Spring MVC
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Implement Solovay Strassen Primality Test Algorithm
The Order of Tests in JUnit
Converting Iterator to List
Java Program to Show the Duality Transformation of Line and Point