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:
A Quick JUnit vs TestNG Comparison
Hướng dẫn Java Design Pattern – State
Dockerizing a Spring Boot Application
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement Jarvis Algorithm
Lớp Collectors trong Java 8
Java Program to Find a Good Feedback Edge Set in a Graph
Anonymous Classes in Java
Java Program to Implement the String Search Algorithm for Short Text Sizes
Comparing Two HashMaps in Java
HttpClient Timeout
Java Program to Find Strongly Connected Components in Graphs
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Perform the Sorting Using Counting Sort
Read an Outlook MSG file
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Remove the First Element from a List
Split a String in Java
Converting Between a List and a Set in Java
Java Program to Implement Counting Sort
Spring Security Custom AuthenticationFailureHandler
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Introduction to Spring Cloud Stream
Java Program to Implement Graph Coloring Algorithm
Hướng dẫn Java Design Pattern – Null Object
Java Program to Implement Tarjan Algorithm
Converting a List to String in Java
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Generate Random Numbers Using Middle Square Method
Java Program to Perform Insertion in a BST
Java Program to Solve a Matching Problem for a Given Specific Case
Introduction to Spring Data JPA