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 8 Collectors toMap
Introduction to the Functional Web Framework in Spring 5
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Tìm hiểu về Web Service
Java Program to Find Path Between Two Nodes in a Graph
Java Program to Find Minimum Element in an Array using Linear Search
Java Program to Find Whether a Path Exists Between 2 Given Nodes
New Features in Java 15
Java – Delete a File
Java Multi-line String
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
How to Convert List to Map in Java
Guide to DelayQueue
New Features in Java 11
Java Copy Constructor
Java Program to Describe the Representation of Graph using Incidence Matrix
Spring Security Logout
Deploy a Spring Boot WAR into a Tomcat Server
Converting Iterator to List
Java – Combine Multiple Collections
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Vector trong Java
Java Program to Implement Hash Trie
Spring Security Authentication Provider
Java equals() and hashCode() Contracts
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Perform Matrix Multiplication
Java Program to Represent Graph Using Adjacency List
Compact Strings in Java 9
Explain about URL and HTTPS protocol
Spring Boot: Customize the Jackson ObjectMapper
Java Program to Implement the Checksum Method for Small String Messages and Detect