Jam J.

asked • 03/13/23

1- Sort this list of numbers 8, 22, 7, 31 , 9 in ascending order using bubble sort- you need to show your work

 End of Pass 1:


End of pass 2:

2 Answers By Expert Tutors

By:

David W. answered • 03/13/23

Tutor
4.7 (90)

Experienced Prof

Juliet C.

tutor
Isn't the number of passes more complicated than n - 1? For example: Original: 4 3 2 9 8 7 After pass 1: 3 2 4 8 7 9 After pass 2: 2 3 4 7 8 9 After pass 3: 2 3 4 7 8 9 No swaps, so algorithm stops.
Report

03/20/23

David W.

Correction: "until p is the index of the last element" should be "until p+1 is the index of the last element NOT YET SORTED."
Report

03/13/23

Juliet C.

tutor
[ Updated to add: I see that you are referring to the optimized version of bubble sort, where the ending index for a pass is decremented by 1 after each pass. ] .......... original comment ......... When p is the index of the last element, that means the last pair of elements in the list have been compared and swapped if necessary. Note that doing one pass through the list does not mean that every element is in its final sorted position, therefore I would not characterize the elements as sorted or not yet sorted. Here is an example of a pass, with square brackets indicating elements being compared since I cannot make them bold in a comment: -- [4 7] 8 2 9 -- -- 4 [7 8] 2 9 -- -- 4 7 [8 2] 9 -- -- 4 7 2 [8 9] --
Report

03/17/23

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.