Below is a listing of a Bubble sort. A. Annotate the program by replacing the bl
ID: 3772360 • Letter: B
Question
Below is a listing of a Bubble sort.
A. Annotate the program by replacing the blue asterisks in lines 15, 21, 24 and 31 (**********) with the correct words/phrases.
B. Literally, what is the output of this program?
1 /*
2 Project 5-1
3 Demonstrate the Bubble sort.
4 */
5
6 class Bubble {
7 public static void main(String args[]) {
8 int nums[] = { 99, -10, 100123, 18, -978,
9 5623, 463, -9, 287, 49 };
10 int a, b, t;
11 int size;
12
13 size = 10; // number of elements to sort
14
15 // *********
16 System.out.print("Original array is:");
17 for(int i=0; i < size; i++)
18 System.out.print(" " + nums[i]);
19 System.out.println();
20
21 // ************
22 for(a=1; a < size; a++)
23 for(b=size-1; b >= a; b--) {
24 if(nums[b-1] > nums[b]) { //********
25 t = nums[b-1];
26 nums[b-1] = nums[b];
27 nums[b] = t;
28 }
29 }
30
31 // **********
32 System.out.print("Sorted array is:");
33 for(int i=0; i < size; i++)
34 System.out.print(" " + nums[i]);
35 System.out.println();
36 }
37 }
Explanation / Answer
1 /*
2 Project 5-1
3 Demonstrate the Bubble sort.
4 */
5
6 class Bubble {
7 public static void main(String args[]) {
8 int nums[] = { 99, -10, 100123, 18, -978,
9 5623, 463, -9, 287, 49 };
10 int a, b, t;
11 int size;
12
13 size = 10; // number of elements to sort
14
15 /* Take the input from the user and put the elements(numbers which have to be sorted) in the array nums[] */
16 System.out.print("Original array is:");
17 for(int i=0; i < size; i++)
18 System.out.print(" " + nums[i]);
19 System.out.println();
20
21 /*perform the bubble sort by omparing pairs of adjacent elements of the sequence, if the they are in the wrong order swap them and do this till there are no swappings to do */
22 for(a=1; a < size; a++)
23 for(b=size-1; b >= a; b--) {
24 if(nums[b-1] > nums[b]) { //********
25 t = nums[b-1];
26 nums[b-1] = nums[b];
27 nums[b] = t;
28 }
29 }
30
31 /* The sorted array after performing the bubble sort is */
32 System.out.print("Sorted array is:");
33 for(int i=0; i < size; i++)
34 System.out.print(" " + nums[i]);
35 System.out.println();
36 }
The output of the program is the sorted list of numbers in ascending orders:
-978 -10 -9 18 49 99 287 463 5623 100123
37 }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.