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:

Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Quick Intro to Spring Cloud Configuration
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Find Basis and Dimension of a Matrix
Configure a RestTemplate with RestTemplateBuilder
How to Get the Last Element of a Stream in Java?
Java Program to Solve TSP Using Minimum Spanning Trees
Guava – Join and Split Collections
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
The Guide to RestTemplate
Java Program to Implement WeakHashMap API
Java Program to Perform Right Rotation on a Binary Search Tree
Working with Network Interfaces in Java
Handling URL Encoded Form Data in Spring REST
Java Program to Perform Uniform Binary Search
Java Program to Check whether Directed Graph is Connected using DFS
Spring Boot Change Context Path
Java Program to Find a Good Feedback Edge Set in a Graph
HashSet trong java
How to Remove the Last Character of a String?
Java Program to implement Bit Matrix
Java Program to Implement Ternary Search Algorithm
Exception Handling in Java
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Spring’s RequestBody and ResponseBody Annotations
More Jackson Annotations
Different Ways to Capture Java Heap Dumps
Serverless Functions with Spring Cloud Function
Introduction to Java Serialization
Tính đóng gói (Encapsulation) trong java
OAuth 2.0 Resource Server With Spring Security 5