Below is a listing of a Bubble sort. A. Annotate the program by replacing the bl
ID: 3761573 • Letter: B
Question
Below is a listing of a Bubble sort.
A. Annotate the program by replacing the blue asterisks in lines 9, 15, 19, 25 (**********) with the correct words/phrases.
B. Re-write the program using the fill and sort methods of the array class.
1/*
2 Demonstrates the Bubble sort.
3*/
4 class Bubble {
5 public static void main(String args[]) {
6 int nums[] = { 99, -10, 100123, 18, -978,
5623, 463, -9, 287, 49 };
7 int a, b, t;
8 int size;
9 size = 10; // number of ******* to sort
10 // display original array
11 System.out.print("Original array is:");
12 for(int i=0; i < size; i++)
13 System.out.print(" " + nums[i]);
14 System.out.println();
15 // This is the *************
16 for(a=1; a < size; a++)
17 for(b=size-1; b >= a; b--) {
18 if(nums[b-1] > nums[b]) { // if out of order
19 // **************
20 t = nums[b-1];
21 nums[b-1] = nums[b];
22 nums[b] = t;
23 }
24 }
25 // display ***********
26 System.out.print("Sorted array is:");
27 for(int i=0; i < size; i++)
28 System.out.print(" " + nums[i]);
29 System.out.println();
30 }
31}
Explanation / Answer
class Bubble {
public static void main(String args[]) {
int nums[] = { 99, -10, 100123, 18, -978,
5623, 463, -9, 287, 49 };
int a, b, t;
int size;
size = 10; // number of ******* to sort
System.out.print("Original array is:");
for(int i=0; i < size; i++)
System.out.print(" " + nums[i]);
System.out.println();
for(a=1; a < size; a++)
for(b=size-1; b >= a; b--)
{
if(nums[b-1] > nums[b]) { // if out of order
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
}
}
System.out.print("Sorted array is:");
for(int i=0; i < size; i++)
System.out.print(" " + nums[i]);
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.