12.2 Sort an array in ascending order Write a program that gets a list of integers from input, and outputs the integers in ascending order (lowest to highest). The first integer indicates how many numbers are in the list. Assume that the list will always contain less than 20 integers. Ex: If the input is:

Respuesta :

Answer:

Explanation:

The following code is written in Java, it asks the user for the number of inputs that will be made and then the inputs. These inputs are placed into an array. Then the array is sorted and printed out to the terminal.

public static void sortArrayList() {

           ArrayList<Integer> sortedList = new ArrayList();

           Scanner input = new Scanner(System.in);

           System.out.println("Enter number of digits in Array");

           int numberOfInputs = input.nextInt();

           for (int i = 0; i < numberOfInputs; i++) {

                   System.out.println("Enter a number");

                   sortedList.add(input.nextInt());

           }

           int n = sortedList.size();

           for (int i = 0; i < n; i++)

               for (int j = 0; j < n-i-1; j++)

                   if (sortedList.get(j) > sortedList.get(j + 1))

                   {

                       // swap arr[j+1] and arr[j]

                       int temp = sortedList.get(j);

                       sortedList.set(j, sortedList.get(j + 1));

                       sortedList.set(j + 1, temp);

                   }

           for (int x : sortedList) {

               System.out.println(x);

           }

       }