Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks user for their input of number of elements to be sorte

ID: 3907501 • Letter: W

Question

Write a program that asks user for their input of number of elements to be sorted and then uses bubbleSort to sort them. An ArrayList must be used to solve this question. Must use the template given below.
//Use Appropriate imports here. public class bubbleSortArrayList { public static void main(String[] args) { ArrayList <Integer> items = new ArrayList<>(); Scanner inp = new Scanner(System.in); //Ask the user for how many elements to be added. System.out.println("Enter the number of elements to be added"); int HowManyElements = inp.nextInt(); //Complete the for loop for adding the elements to the ArrayList. for (int i = 1 ; i <=; i++){ } //Print out the Original ArrayList after user input Here. /* Call the non-static bubbleSort method using the instantiated object ‘list’ of the class it is defined list.bubbleSort(items); } /*Complete the BubbleSort Algorithm as per the ArrayList usage and methods. Remember, the way to access the index elements is different in arrayLists than in Arrays. public void bubbleSort (){ for (){ } } Write a program that asks user for their input of number of elements to be sorted and then uses bubbleSort to sort them. An ArrayList must be used to solve this question. Must use the template given below.
//Use Appropriate imports here. public class bubbleSortArrayList { public static void main(String[] args) { ArrayList <Integer> items = new ArrayList<>(); Scanner inp = new Scanner(System.in); //Ask the user for how many elements to be added. System.out.println("Enter the number of elements to be added"); int HowManyElements = inp.nextInt(); //Complete the for loop for adding the elements to the ArrayList. for (int i = 1 ; i <=; i++){ } //Print out the Original ArrayList after user input Here. /* Call the non-static bubbleSort method using the instantiated object ‘list’ of the class it is defined list.bubbleSort(items); } /*Complete the BubbleSort Algorithm as per the ArrayList usage and methods. Remember, the way to access the index elements is different in arrayLists than in Arrays. public void bubbleSort (){ for (){ } } Write a program that asks user for their input of number of elements to be sorted and then uses bubbleSort to sort them. An ArrayList must be used to solve this question. Must use the template given below.
//Use Appropriate imports here. public class bubbleSortArrayList { public static void main(String[] args) { ArrayList <Integer> items = new ArrayList<>(); Scanner inp = new Scanner(System.in); //Ask the user for how many elements to be added. System.out.println("Enter the number of elements to be added"); int HowManyElements = inp.nextInt(); //Complete the for loop for adding the elements to the ArrayList. for (int i = 1 ; i <=; i++){ } //Print out the Original ArrayList after user input Here. /* Call the non-static bubbleSort method using the instantiated object ‘list’ of the class it is defined list.bubbleSort(items); } /*Complete the BubbleSort Algorithm as per the ArrayList usage and methods. Remember, the way to access the index elements is different in arrayLists than in Arrays. public void bubbleSort (){ for (){ } }

Explanation / Answer

Java program:

//Use Appropriate imports here.

import java.util.ArrayList;

import java.util.Scanner;

public class bubbleSortArrayList

{

public static void main(String[] args)

{

ArrayList <Integer> items = new ArrayList<>();

Scanner inp = new Scanner(System.in);

//Ask the user for how many elements to be added.

System.out.println("Enter the number of elements to be added");

int HowManyElements = inp.nextInt();

System.out.println("Enter "+HowManyElements+" elements: ");

//Complete the for loop for adding the elements to the ArrayList.

for (int i = 1 ; i <=HowManyElements; i++)

{

int element=inp.nextInt();

items.add(element);

}

//Print out the Original ArrayList after user input Here.

System.out.println("Original ArrayList:");

for(Integer i: items)

{

System.out.print(i+" ");

}

/* Call the non-static bubbleSort method using the

* instantiated object ‘list’ of the class it is defined

*/

bubbleSortArrayList list = new bubbleSortArrayList();

list.bubbleSort(items);

}

/*Complete the BubbleSort Algorithm as per the ArrayList usage and methods.

* Remember, the way to access the index elements is different in arrayLists than in Arrays.

*/

public void bubbleSort (ArrayList<Integer> list)

{

System.out.println(" After Bubble Sort:");

//sorting logic starts from here

for(int i = list.size()-1; i >= 0; i--)

{

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

{

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

{

//swapping-out

int temp = list.get(j);

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

list.set(j + 1, temp);

}

}

}

//printing elements after sorting

for(Integer el : list)

{

System.out.print(el +" ");

}

}

}

Output:

Enter the number of elements to be added
5
Enter 5 elements:
13
10
14
12
16
Original ArrayList:
13 10 14 12 16
After Bubble Sort:
10 12 13 14 16

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote