Question
These questions deal with the basic bubble sort code in Java. pass=0; do { pass++; swapped = false; for (int pos = 0; pos <
These questions deal with the basic bubble sort code in Java.
pass=0;
do {
pass++;
swapped = false;
for (int pos = 0; pos < n - pass; pos++)
if (numb[pos] > numb[pos+1]) {
temp = numb[pos];
numb[pos] = numb[pos+1];
numb[pos+1] = temp;
swapped = true;
}
} while(swapped == true);
In writing, predict the answers to the questions below:
What would happen if we omitted the statement swapped = true; ? (We would use just three statements instead of the four shown in the body of the if above.)
What would happen if we used the following condition instead of what is shown above?
} while (swapped); // This is more elegant.
What would happen if we used the following if condition to compare two items?
if(numb[pos] > numb[pos] + 1) Do you see what is different?
What would happen if we tried to interchange two items using these two statements inside the brackets { } (instead of the first three shown above)?
numb[pos] = numb[pos+1];
numb[pos+1] = numb[pos];
What would happen if we omitted swapped = false; ? What if the array was initially in sorted order? What if at least one value was out of order?
What would happen if we added an else swapped =false; to the if statement.
Write a driver for the above code. You may initialize the array in the declaration. You may put the BubbleSortcode in main() or in another method. In each case (a f), write a little program which tests what actually happens. (SaveAs all the versions of your program. Remember to start with original correct version each time.) For each part, run the program
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started