Skip to content
Learnearn.uk » IB Computer Science » Bubble Sort

Bubble Sort

Introduction

Bubble Sort

Time Complexity: O(n²) – Space Complexity: O(1)

Pretty much the worst sorting algorithm ever, mostly just used in teaching as a comparison tool. There are almost no legitimate use cases where it is the most efficient algorithm.

The bubble sort algorithm works by sorting through the array in pairs. It starts at the left of the array and inspects the first two items. If the items are in the wrong order they are swapped around. The algorithm then carries on with the next pair along and so forth. When it reaches the end of the array it goes back to the beginning of the array and starts again. If it completes a full pass of the array without swapping any items it knows that the array is sorted.

Bubble sort animation. Credit - Wikipedia

Tutorial Video

Bubble Sort Python Tutorial

Demonstration

Bubble Sort Demonstration

Python Code

Python Code

 

Resources