How many passes does a Bubble sort algorithm require for sorting a given list of 'n' items?

This question was previously asked in
ESE Electronics 2011 Paper 2: Official Paper
View all UPSC IES Papers >
  1. n2
  2. √n
  3. n + 1
  4. n - 1

Answer (Detailed Solution Below)

Option 4 : n - 1
Free
ST 1: UPSC ESE (IES) Civil - Building Materials
6.1 K Users
20 Questions 40 Marks 24 Mins

Detailed Solution

Download Solution PDF

Concept:

Bubble sort:

It is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.

Compare the neighbours, if greater, swap.

E.g. Let’s i / p is 70, 20, 35, 90, 15, 11, 24

No. of elements (n) = 7

Pass – 1: 20, 35, 70, 15, 11, 24, } 90 → 6 comparison

Pass – 2: 20, 35, 15, 11, 24, } 70, 90 → 5 comparison

Pass – 3: 20, 15, 11, 24, } 35, 70, 90 → 4 comparison

Pass – 4: 15, 11, 20, } 24, 35, 70, 90 → 3 comparison

Pass – 5: 11, 15, } 20, 24, 35, 70, 90 → 2 comparison

Pass – 6: 11, 15, 20, 24, 35, 70, 90 → 1 comparison

No. of passes = 6

Note: Bubble sort with n element required n - 1 passes.

Total comparisons: \(\frac{{\left( {n - 1} \right)n}}{2}\)

Latest UPSC IES Updates

Last updated on May 28, 2025

->  UPSC ESE admit card 2025 for the prelims exam has been released. 

-> The UPSC IES Prelims 2025 will be held on 8th June 2025.

-> The selection process includes a Prelims and a Mains Examination, followed by a Personality Test/Interview.

-> Candidates should attempt the UPSC IES mock tests to increase their efficiency. The UPSC IES previous year papers can be downloaded here.

More Sorting Questions

Get Free Access Now
Hot Links: teen patti wala game teen patti master real cash happy teen patti