Question
Write the following method that sorts an ArrayList of numbers. public static void sort(ArrayList
Solution
VerifiedAnswered 5 months ago
Answered 5 months ago
Step 1
1 of 3import java.math.BigDecimal;
import java.util.*;
public class Sort {
public static void main(String[] args) {
ArrayList<Number> tst = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.println("Enter five Numbers");
for (int i = 0; i < 5; i++) {
tst.add(input.nextDouble());
}
input.close();
sort(tst);
System.out.println(tst);
}
/** bubble sort */
public static void sort(ArrayList<Number> list){
Number temp;
for (int x=0; x<list.size(); x++){
for (int i=0; i < list.size()-x-1; i++) {
if (compareTo(list.get(i), list.get(i+1)) > 0){
temp = list.get(i);
list.set(i,list.get(i+1) );
list.set(i+1, temp);
}
}
}
}
private static int compareTo(Number n1, Number n2) {
// ignoring null handling
BigDecimal b1 = new BigDecimal(n1.doubleValue());
BigDecimal b2 = new BigDecimal(n2.doubleValue());
return b1.compareTo(b2);
}
}
Create a free account to view solutions
By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Create a free account to view solutions
By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Recommended textbook solutions

Computer Organization and Design MIPS Edition: The Hardware/Software Interface
5th Edition•ISBN: 9780124077263David A. Patterson, John L. Hennessy226 solutions

Intro to Java Programming, Comprehensive Version
10th Edition•ISBN: 9780133761313Y. Daniel Liang1,628 solutions

Fundamentals of Database Systems
7th Edition•ISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe948 solutions

Introduction to Algorithms
3rd Edition•ISBN: 9780262033848Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen872 solutions
More related questions
1/4
1/7