Java Program to Find Second Smallest of n Elements with 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.

  1. //This is a java program to find the second smallest element of N elements with the minimum space complexity constraints 
  2. import java.util.Random;
  3.  
  4. public class Second_Smallest_Element 
  5. {
  6.     static int kthminimum(int[] sequence, int k) 
  7.     {
  8.         // Bubble Sort for length of sequence minus k times
  9.         for (int i = 0; i < (sequence.length - k); i++)
  10.             for (int j = 0; j < sequence.length - 1; j++)
  11.                 if (sequence[j] > sequence[j + 1]) 
  12.                 {
  13.                     sequence[j] = sequence[j] + sequence[j + 1];
  14.                     sequence[j + 1] = sequence[j] - sequence[j + 1];
  15.                     sequence[j] = sequence[j] - sequence[j + 1];
  16.                 }
  17.         return sequence[k - 1];
  18.     }
  19.  
  20.     public static void main(String args[]) 
  21.     {
  22.         Random random = new Random();
  23.         int N = 20;
  24.         int[] sequence = new int[N];
  25.  
  26.         for (int i = 0; i < N; i++)
  27.             sequence[i] = Math.abs(random.nextInt(1000));
  28.  
  29.         System.out.println("Original Sequence: ");
  30.         for (int i = 0; i < N; i++)
  31.             System.out.print(sequence[i] + " ");
  32.  
  33.         System.out.println("\nSecond smallest element :\n"
  34.                 + kthminimum(sequence, 2));
  35.     }
  36. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.