HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Problems
  2. /A bubble sort puts the scores 5, 1, 4, 2, 8 into ascending order…
MediumComputer ScienceHigh School (9–10)Computer Science › Algorithms and Problem-SolvingNumericalIGCSE

A bubble sort puts the scores 5, 1, 4, 2, 8 into ascending order…

StemPy LabContributed by StemPy Lab

Try it on paper first. A hint, the key idea and the worked solution are here when you need them.

A bubble sort puts the scores 555, 111, 444, 222, 888 into ascending order. It stops after a pass that makes no swaps.

  1. Write the list after the first pass. How many swaps does the first pass make?
  1. How many passes does the sort make in all, including the last pass with no swaps?

Check your answer

Work it out on paper, then type your number. The answer itself stays hidden.

Need a reminder? The key ideaShowHide

Linear search: compare each item in turn from the first; stop when it is found or the list ends. Bubble sort: compare each pair of neighbours and swap them if they are in the wrong order; repeat passes until a pass makes no swaps. A pass over nnn items makes n−1n - 1n−1 comparisons at most.

The worked solution

Have you tried it?

Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.

Step 1. Pass 1. 5,15, 15,1: swap, giving 1,5,4,2,81, 5, 4, 2, 81,5,4,2,8. 5,45, 45,4: swap, giving 1,4,5,2,81, 4, 5, 2, 81,4,5,2,8. 5,25, 25,2: swap, giving 1,4,2,5,81, 4, 2, 5, 81,4,2,5,8. 5,85, 85,8: no swap. So 333 swaps, and the list is 1,4,2,5,81, 4, 2, 5, 81,4,2,5,8.

Step 2. Pass 2. 1,41, 41,4: no swap. 4,24, 24,2: swap, giving 1,2,4,5,81, 2, 4, 5, 81,2,4,5,8. 4,54, 54,5: no swap. One swap.

Step 3. Pass 3. 1,21, 21,2 and 2,42, 42,4: no swaps. A pass with no swaps, so the sort stops.

Answer: (a) 1,4,2,5,81, 4, 2, 5, 81,4,2,5,8 after 333 swaps; (b) 333 passes.

Check. The list was sorted after pass 2, but the algorithm only knows this when pass 3 makes no swap.

Log in to save this for later
← All Problems
← A club keeps its eight members in an array in the order they joined: Ines, Kofi, Hana, Luis, Ama…A shop stores the colours of the ten bicycles in its window in an array Colours: red, blue, red… →
More onComputer Science › Algorithms and Problem-Solving

Discussion (0)

Log in to join the discussion.

Loading…

Learn it in

  1. Linear search and bubble sortStandard Algorithms: Totals, Searching, Sorting and Validation

More on this topic

  1. This is the kind of long scenario question that ends Paper 2…Hard
  2. A delivery company adds a check digit to each parcel number…Medium
  3. Name the check in each case. Say whether it is validation or verification…Easy
A bubble sort is changed so that it sorts in descending order: the comparison becomes IF…Hard
  • A shop stores the colours of the ten bicycles in its window in an array Colours: red, blue, red…Medium
  • All problems on this topic →