Problem

(Bubble sort) Write a sort function that uses the bubble-sort algorithm. The algorithm mak...

(Bubble sort) Write a sort function that uses the bubble-sort algorithm. The algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is in decreasing order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually “bubble” their way to the top and the larger values sink to the bottom.

The algorithm can be described as follows:

bool changed = true ;do  {changed = false ;for (int j = 0 ; j1 ; j++) if (1ist[j] > 1ist[j + 1 ]) { swap 1ist[j] with 1ist[j + 1 ]; changed = true ; }} while (changed);

Clearly, the list is in increasing order when the loop terminates. It is easy to show that the do loop executes at most listSize − 1 times.

Write a test program that reads in an array of ten double numbers, invokes the function, and displays the sorted numbers.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT