AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-collection: Sorting using the given conditions

Correct the Program given below that will read a sequence of positive real numbers entered by the user and will print the same numbers in sorted order from smallest to largest. The user will input a zero to mark the end of the input. Assume that at most 100 positive numbers will be entered. Sample Input: 56 48 89 14 29 65 34 67 2 4 44 0 Sample output: 2 4 14 29 34 44 48 56 65 67 89 import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class sample{ public static void main(String args[]) throws IOException{ List<Integer> inputList = new ArrayList<Integer>(); Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int no = sc.nextInt(); if(no==0){ break; } inputList.add(no); } for(int i=0;i<inputList.size();i++){ int temp = 0; for(int j=0;j<inputList.size();j++){ if(inputList.get(i)<inputList.get(j)){ // logic goes here } } } for(int i=0;i<inputList.size();i++){ // code goes here } } }

corejava x 353
collection x 52
Posted On : 2017-02-10 18:30:45.0
profile Raksha - anyforum.in Raksha
2510
up-rate
4
down-rate

Answers


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Sample{
public static void main(String args[]) throws IOException{
List<Integer> inputList = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int no = sc.nextInt();
if(no==0){ break; }
inputList.add(no);
}
for(int i=0;i<inputList.size();i++){
int temp = 0;
for(int j=i+1;j<inputList.size();j++){
if(inputList.get(i)>inputList.get(j)){
temp = inputList.get(i);
inputList.set(i,inputList.get(j));
inputList.set(j,temp);
}
}
}
for(int i=0;i<inputList.size();i++){
System.out.print(inputList.get(i)+" ");
}
}
}

Posted On : 2017-02-11 00:47:26
Satisfied : 3 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188222689
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in