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 Stream findFirst() vs. findAny()
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Implement Hash Trie
Java Program to Implement Heap Sort Using Library Functions
Handling URL Encoded Form Data in Spring REST
Java Program to Implement Randomized Binary Search Tree
Spring Boot Annotations
Java Program to Implement Stack using Linked List
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Remove the First Element from a List
Apache Tiles Integration with Spring MVC
Java Program to Check Whether a Given Point is in a Given Polygon
Guide to the Synchronized Keyword in Java
Java Program to Construct an Expression Tree for an Postfix Expression
Java Program to Perform Polygon Containment Test
String Processing with Apache Commons Lang 3
A Quick Guide to Spring MVC Matrix Variables
Consumer trong Java 8
Marker Interface trong Java
Converting Iterator to List
Interface trong Java 8 – Default method và Static method
Create a Custom Exception in Java
Giới thiệu Aspect Oriented Programming (AOP)
Jackson – Change Name of Field
Java Program to Implement Rope
Spring Boot - Internationalization
Spring @Primary Annotation
Java Program to Implement Bit Array
Count Occurrences of a Char in a String
Java Program to Find Nearest Neighbor for Dynamic Data Set
Overflow and Underflow in Java