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 is changed so that it sorts in descending order: the comparison becomes IF…
HardComputer ScienceHigh School (9–10)Computer Science › Algorithms and Problem-SolvingNumericalConceptualIGCSE

A bubble sort is changed so that it sorts in descending order: the comparison becomes IF…

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 is changed so that it sorts in descending order: the comparison becomes IF Scores[Index] < Scores[Index + 1]. It sorts 333, 888, 666, 999, 222, and stops after a pass with no swaps. Each pass compares one pair fewer than the pass before.

  1. What is the list after the first pass?
  1. How many comparisons does the first pass make?
  1. How many passes does the sort make in all?

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 (4 comparisons). 3<83 < 83<8: swap, giving 8,3,6,9,28, 3, 6, 9, 28,3,6,9,2. 3<63 < 63<6: swap, giving 8,6,3,9,28, 6, 3, 9, 28,6,3,9,2. 3<93 < 93<9: swap, giving 8,6,9,3,28, 6, 9, 3, 28,6,9,3,2. 3<23 < 23<2: no. The list is 8,6,9,3,28, 6, 9, 3, 28,6,9,3,2.

Step 2. Pass 2 (3 comparisons). 8<68 < 68<6: no. 6<96 < 96<9: swap, giving 8,9,6,3,28, 9, 6, 3, 28,9,6,3,2. 6<36 < 36<3: no.

Step 3. Pass 3 (2 comparisons). 8<98 < 98<9: swap, giving 9,8,6,3,29, 8, 6, 3, 29,8,6,3,2. 8<68 < 68<6: no. A swap happened, so the sort goes on.

Step 4. Pass 4 (1 comparison). 9<89 < 89<8: no. No swaps, so the sort stops.

Answer: (a) 8,6,9,3,28, 6, 9, 3, 28,6,9,3,2; (b) 444 comparisons; (c) 444 passes.

Check. The list 9,8,6,3,29, 8, 6, 3, 29,8,6,3,2 was ready after pass 3, but the sort only knows this after a pass with no swaps. Pass 1 moved the smallest value to the end, as a descending sort should.

Log in to save this for later
← All Problems
← A shop stores the colours of the ten bicycles in its window in an array Colours: red, blue, red…Name the check in each case. Say whether it is validation or verification… →
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 shop stores the colours of the ten bicycles in its window in an array Colours: red, blue, red…Medium
  • A bubble sort puts the scores 5, 1, 4, 2, 8 into ascending order…Medium
  • All problems on this topic →