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
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. package com.instanceofjava; class SecondSmallestNumber{ public static void main(String args[]) { int numbers[] = {6,3,37,12,46,5,64,21}; int smallest= numbers[0];; int second_smallest = numbers[0];; for(int n:numbers){ if(n< smallest){ second_smallest = smallest; smallest=n; } else if( n< second_smallest){ second_smallest= n; } } System.out.println("Smallest Number: "+smallest); System.out.println("Second Smallest Number: "+second_smallest); } }
Output: Smallest Number: 3 Second Smallest Number: 5 Java interview Program to find second Smallest number in an integer array by sorting the elements. package com.instanceofjava; class SecondSmallestNumber{ public static void main(String args[]) { int numbers[] = {6,3,37,12,46,5,64,21}; Arrays.sort(numbers); System.out.println("Smallest Number: "+numbers[0]); System.out.println("Second Smallest Number: "+numbers[1]); } } Output: Smallest Number: 3 Second Smallest Number: 5
Read: Find Second smallest number in java without sorting