Java Program to Find Second Smallest of n Elements with Given Complexity Constraint

This is a java program to find the second smallest element with given complexity. Complexity here is minimum space constraints. Inplace sorting and returning second element help achieving the space constraints.

Here is the source code of the Java Program to Find Second Smallest of n Elements with Given Complexity Constraint. 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 second smallest element of N elements with the minimum space complexity constraints 
import java.util.Random;
 
public class Second_Smallest_Element 
{
    static int kthminimum(int[] sequence, int k) 
    {
        // Bubble Sort for length of sequence minus k times
        for (int i = 0; i < (sequence.length - k); i++)
            for (int j = 0; j < sequence.length - 1; j++)
                if (sequence[j] > sequence[j + 1]) 
                {
                    sequence[j] = sequence[j] + sequence[j + 1];
                    sequence[j + 1] = sequence[j] - sequence[j + 1];
                    sequence[j] = sequence[j] - sequence[j + 1];
                }
        return sequence[k - 1];
    }
 
    public static void main(String args[]) 
    {
        Random random = new Random();
        int N = 20;
        int[] sequence = new int[N];
 
        for (int i = 0; i < N; i++)
            sequence[i] = Math.abs(random.nextInt(1000));
 
        System.out.println("Original Sequence: ");
        for (int i = 0; i < N; i++)
            System.out.print(sequence[i] + " ");
 
        System.out.println("\nSecond smallest element :\n"
                + kthminimum(sequence, 2));
    }
}

Output:

$ javac Second_Smallest_Element.java
$ java Second_Smallest_Element
 
Original Sequence: 
459 886 873 766 616 878 122 372 453 876 845 965 477 139 788 861 148 5 894 439 
Second smallest element :
122
 
Original Sequence: 
695 213 257 62 315 289 234 90 153 721 192 183 676 373 292 928 57 472 200 177 
Second smallest element :
62

Related posts:

Calling Stored Procedures from Spring Data JPA Repositories
Spring Security – security none, filters none, access permitAll
Java Program to Represent Graph Using Linked List
Getting Started with Forms in Spring MVC
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java Program to Implement Merge Sort Algorithm on Linked List
Guide to Java Instrumentation
Spring Boot - Tomcat Port Number
Hướng dẫn Java Design Pattern – Object Pool
Java Program to Implement K Way Merge Algorithm
Phương thức forEach() trong java 8
Java Program to Implement Fermat Primality Test Algorithm
Java Program to Check whether Undirected Graph is Connected using DFS
New in Spring Security OAuth2 – Verify Claims
Removing Elements from Java Collections
Validate email address exists or not by Java Code
Spring Boot - Batch Service
Java Program to Implement WeakHashMap API
How to Manually Authenticate User with Spring Security
Control Structures in Java
Spring Boot - Securing Web Applications
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Spring Boot - Web Socket
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
A Quick Guide to Using Keycloak with Spring Boot
Spring Data JPA Delete and Relationships
ClassNotFoundException vs NoClassDefFoundError
MyBatis with Spring
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
How to Get a Name of a Method Being Executed?
Cachable Static Assets with Spring MVC