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:
Build a REST API with Spring and Java Config
Java 9 Stream API Improvements
Allow user:password in URL
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Implement K Way Merge Algorithm
Runnable vs. Callable in Java
Java Program to Implement CopyOnWriteArrayList API
Testing in Spring Boot
How to Find an Element in a List with Java
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Java Program to Perform Insertion in a BST
Spring 5 Testing with @EnabledIf Annotation
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program for Topological Sorting in Graphs
Hướng dẫn Java Design Pattern – Dependency Injection
Guide to Spring 5 WebFlux
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Guide to @ConfigurationProperties in Spring Boot
The Dining Philosophers Problem in Java
Arrays.asList vs new ArrayList(Arrays.asList())
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Generate Random Hexadecimal Byte
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Deploy a Spring Boot App to Azure
Hướng dẫn Java Design Pattern – Proxy
Using a List of Values in a JdbcTemplate IN Clause
Testing an OAuth Secured API with Spring MVC
Ép kiểu trong Java (Type casting)
Java Program to Implement Sieve Of Eratosthenes
Java Program to Implement Double Order Traversal of a Binary Tree
Introduction to Liquibase Rollback
So sánh ArrayList và Vector trong Java