Try the fastest way to create flashcards
Question

# Write the following method that sorts an ArrayList of numbers. public static void sort(ArrayList list)

Solution

Verified
Step 1
1 of 3
import 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++) {
}

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);
}
}


## Recommended textbook solutions #### Computer Organization and Design MIPS Edition: The Hardware/Software Interface

5th EditionISBN: 9780124077263David A. Patterson, John L. Hennessy
226 solutions #### Intro to Java Programming, Comprehensive Version

10th EditionISBN: 9780133761313Y. Daniel Liang
1,628 solutions #### Fundamentals of Database Systems

7th EditionISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe
948 solutions #### Introduction to Algorithms

3rd EditionISBN: 9780262033848Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
872 solutions