Skip to main content
Informatik.uz
Chapter 1. Algorithms

Sorting algorithms: bubble sort

Sorting puts data in order and speeds up searching. We study bubble sort, trace it step by step and see why it is slow on big arrays.

Topic 1 of 112 min readUpdated October 27, 2026
All topics (1 of 11)

Sorting means arranging elements by some rule, for example in ascending or descending order. In ordered data a required element is found much faster, and lists are easier to read. There are many sorting methods; the simplest and clearest is bubble sort. In Python, an array is represented by a list (list).

The idea of bubble sort is this: compare two neighboring elements and swap them if they are in the wrong order. After one pass through the array from start to end, the largest element “floats” to the end, like a bubble in water. In the next pass the last element is no longer checked because it is already in place, so the checked part shrinks by one each time.

Let the array be 5 2 4 1. After pass 1 it becomes 2 4 1 5, after pass 2 it is 2 1 4 5, and after pass 3 it is 1 2 4 5. In Python, neighbors are compared with if a[j] > a[j + 1] and swapped with a[j], a[j + 1] = a[j + 1], a[j]; these actions repeat inside two nested for loops. If a pass makes no swaps at all, the array is already sorted and the process can stop.

Sorting n elements takes about n(n − 1)/2 comparisons, so the time grows in proportion to n². For 1000 elements that is about 500 thousand comparisons, and for 10,000 elements about 50 million. So bubble sort is good for learning but inefficient for big data. In practice, Python’s ready-made sorted() and list.sort() are used: they rely on a much faster algorithm (Timsort).

Key takeaways

  • Sorting arranges elements in ascending or descending order.
  • Bubble sort compares neighboring elements and swaps those that are in the wrong order.
  • With each pass the largest remaining element moves to its place at the end.
  • A pass with no swaps means the array is sorted, so the process can stop.
  • About n(n − 1)/2 comparisons are needed, so the method is slow on large arrays.

Practice tasks

  1. Sort the array 7 3 9 1 5 by hand with bubble sort: write down the state after each pass and count the comparisons made.
  2. Write a Python bubble sort program that sorts a list of numbers entered by the user in ascending order.
  3. Add a stop condition for a pass with no swaps, then compare the number of comparisons for an already sorted list and for a reversed one.

Review questions

Test yourself: think first, then reveal the answer.

  1. 1Which element is guaranteed to reach its place after one pass of bubble sort?
    The largest one: it moves to the end of the array.
  2. 2Why is bubble sort unsuitable for large arrays?
    The number of comparisons grows with the square of the number of elements: 10,000 elements need about 50 million operations.
  3. 3How many passes does the version with an early stop (no swaps) make on an already sorted array?
    Just one: it finds no swaps, so the process stops.

0 of 11 topics completed

Sign in to mark
Informatika 11-sinf darsligi muqovasi
In the textbookInformatics — Grade 11