file: https://docs.google.com/document/d/16o52SASnkr0nz2C1_kcQKa5sU1_5mKIm1cT9Qo
ID: 3683501 • Letter: F
Question
file: https://docs.google.com/document/d/16o52SASnkr0nz2C1_kcQKa5sU1_5mKIm1cT9Qo1bZMI/edit?usp=sharing
Start with the file TemplateLab10Bronze. java. Complete the printlnOrder method at the end of the file. This method will accept ArrayList data as its parameter. It will not sort this list. Instead, it will use a modified selection sort algorithm to simply print the list in ascending order. The list should be printed on a single line. As each number is printed, it can be deleted from data, so that when the method is finished, the list data will be an empty list. The output from the program should look like this. (The data will be random.) Original list: [27, 44, 22, 20, 41, 29, 1, 2, 33, 7, 16, 26, 13, 44, 15, 15, 23, 11, 42, 13] Print the list in ascending order: 2 7 1 11 13 13 15 15 16 20 22 23 26 27 29 33 41 42 44 44 Original list (should be empty): []Explanation / Answer
import java.util.ArrayList;
public class TemplateLab10Bronze {
//Constants controlling the random test data
static final int LIST_SIZE = 20;
static final int MAX_NUMBER = 50;
public static void main(String[] args) {
//Create a random list of integers
ArrayList<Integer> test = new ArrayList<Integer>();
for(int i=0; i<LIST_SIZE; i++)
test.add((int)(Math.random()*MAX_NUMBER));
//Print the list, before and after testing the
//printInOrder method.
System.out.println("Original list: "+test);
System.out.println("Print the list in ascending order:");
printInOrder(test);
System.out.println("Original list (should be empty): "+test);
}//main
public static void printInOrder(ArrayList<Integer> data){
//Print out the elements of the ArrayList in ascending order
//The ArrayList will be destroyed in the process, becoming empty.
int len = data.size();
for(int j=0;j<len;j++)
{
int min = data.get(0);
int place = 0;
for (int i = 0; i < data.size(); i++)
{
if(data.get(i)<min)
{
min = data.get(i);
place = i;
}
}
System.out.print(min);
System.out.print(" ");
data.remove(place);
}
System.out.println();
}//printInOrder
}//TemplateLab10Bronze
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.