The Artima Developer Community
Sponsored Link

Java Buzz Forum
Find Second smallest number in java without sorting

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Find Second smallest number in java without sorting Posted: Feb 15, 2016 5:39 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Find Second smallest number in java without sorting
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

Java interview Program to find second smallest number in an integer array without sorting the elements.


  1. package com.instanceofjava;
  2. class SecondSmallestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7.  
  8. int smallest=  numbers[0];;
  9.  int second_smallest =  numbers[0];;
  10.  
  11. for(int n:numbers){
  12.  
  13. if(n< smallest){
  14.  
  15.      second_smallest = smallest;
  16.       smallest=n;
  17.  
  18.  } else if( n< second_smallest){
  19.  
  20.                 second_smallest= n;
  21.  
  22. }
  23.  
  24. }
  25.          System.out.println("Smallest Number: "+smallest);
            System.out.println("Second Smallest Number: "+second_smallest);

  26.  
  27.  }
  28.  
  29. }



Output:
 
  1. Smallest Number: 3
  2. Second Smallest Number: 5

Java interview  Program to find second Smallest number in an integer array by sorting the elements.


  1. package com.instanceofjava;
  2. class SecondSmallestNumber{
  3.  
  4. public static void main(String args[])
  5.  
  6. int numbers[] = {6,3,37,12,46,5,64,21};
  7.  
  8.   Arrays.sort(numbers);
  9.  
  10.   System.out.println("Smallest Number: "+numbers[0]);
  11.   System.out.println("Second Smallest Number: "+numbers[1]);

  12.  }
  13.  
  14. }


Output:
 
  1. Smallest Number: 3
  2. Second Smallest Number: 5



Read: Find Second smallest number in java without sorting

Topic: Dear API Designer. Are You Sure, You Want to Return a Primitive? Previous Topic   Next Topic Topic: Make Your Factories Beautiful

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use