ArticleslgStudy

science

Bubble sort

Bubble sort is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bubble sort rather than just read about it. In short: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed. These passes through the list are repeated until no swaps have to be performed during a pass, meaning that the list has become fully sorted.

Bubble sort — main illustration
Bubble sort — illustration

Key takeaways

  • Bubble sort belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bubble sort to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bubble sort from memory before moving on to harder problems.

Reference excerpt

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed. These passes through the list are repeated until no swaps have to be performed during a pass, meaning that the list has become fully sorted. The algorithm, which is a comparison sort, is named for the way the larger elements "bubble" up to the top of the list. It performs poorly in real-world use and is used primarily as an educational tool. More efficient algorithms such as quicksort, Timsort, or merge sort are used by the sorting libraries built into popular programming languages such as Python and Java.

History The earliest description of the bubble sort algorithm was in a 1956 paper by mathematician and actuary Edward Harry Friend, Sorting on electronic computer systems, published in the third issue of the third volume of the Journal of the Association for Computing Machinery (ACM), as a "Sorting exchange algorithm." Friend described the fundamentals of the algorithm, and although initially his paper went unnoticed, some years later it was rediscovered by many computer scientists, including Kenneth E. Iverson, who coined its current name.

Analysis

Performance Bubble sort has a worst-case and average complexity of O(n2), where n is the number of items being sorted. Most practical sorting algorithms have substantially better worst-case or average complexity, often O(nlog⁡n). Even other O(n2) sorting algorithms, such as insertion sort, generally run faster than bubble sort, and are no more complex. For this reason, bubble sort is rarely used in practice. Like insertion sort, bubble sort is adaptive, which can give it an advantage over algorithms like quicksort. This means that it may outperform those algorithms in cases where the list is already mostly sorted (having a small number of inversions), despite the fact that it has worse average-case time complexity. For example, bubble sort is O(n) on a list that is already sorted, while quicksort would still perform its entire O(nlog⁡n) sorting process. While any sorting algorithm can be made O(n) on a presorted list simply by checking the list before the algorithm runs, improved performance on almost-sorted lists is harder to replicate.

Rabbits and turtles The distance and direction that elements must move during the sort determine bubble sort's performance because elements move in different directions at different speeds. An element that must move toward the end of the list can move quickly because it can take part in successive swaps. For example, the largest element in the list will win every swap, so it moves to its sorted position on the first pass even if it starts near the beginning. On the other hand, an element that must move toward the beginning of the list cannot move faster than one step per pass, so elements move toward the beginning very slowly. If the smallest element is at the end of the list, it will take n−1 passes to move it to the beginning. This has led to these types of elements being named rabbits and turtles, respectively, after the characters in Aesop's fable of The Tortoise and the Hare. Various efforts have been made to eliminate turtles to improve the speed of bubble sort. Cocktail sort is a bi-directional bubble sort that goes from beginning to end, and then reverses itself, going end to beginning. It can move turtles fairly well, but it retains O(n2) worst-case complexity. Comb sort compares elements separated by large gaps, and can move turtles extremely quickly before proceeding to smaller and smaller gaps to smooth out the list. Its average speed is comparable to faster algorithms like quicksort.

Step-by-step example Take an array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required:

First pass ( 5 1 4 2 8 ) → ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) → ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8 ) → ( 1 4 2 5 8 ), Swap since 5 > 2 ( 1 4 2 5 8 ) → ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. Second pass ( 1 4 2 5 8 ) → ( 1 4 2 5 8 ) ( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), Swap since 4 > 2 ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one additional whole pass without any swap to know it is sorted.

Third pass ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) → ( 1 2 4 5 8 ) ( 1 2 4 5 8 ) → ( 1 2 4 5 8 )

Implementation

Pseudocode implementation In pseudocode the algorithm can be expressed as (0-based array):

Optimizing bubble sort

The bubble sort algorithm can be optimized by observing that the n-th pass finds the n-th largest element and puts it into its final place. So, the inner loop can avoid looking at the last n − 1 items when running for the n-th time:

More generally, it can happen that more than one element is placed in its final position on a single pass. In particular, after every pass, all elements after the last swap are sorted, and do not need to be checked again. This allows us to skip over many elements, resulting in about a 50% improvement in the worst-case comparison count (though no improvement in swap counts), and adds very little complexity because the new code subsumes the swapped variable: To accomplish this in pseudocode, the following can be written:

Alternate modifications, such as the cocktail shaker sort attempt to improve on the bubble sort performance while keeping the same idea of repeatedly comparing and swapping adjacent items.

Use

… excerpt ends here. Continue reading the full article.

Illustrations

Bubble sort illustration
Bubble sort: An example of bubble sort. Starting from the beginning of the list, compare every adjacent pair, swapping their position if they are not in the right order (the latter one is smaller than the former one). After each iteration, one less element (the last one) needs to be compared until there are no more elements left to be compared.
An example of bubble sort. Starting from the beginning of the list, compare every adjacent pair, swapping their position if they are not in the right order (the latter one is smaller than the former one). After each iteration, one less element (the last one) needs to be compared until there are no more elements left to be compared.
Bubble sort: Bubble sort. The list was plotted in a Cartesian coordinate system, with each point (x, y) indicating that the value y is stored at index x. Then the list would be sorted by bubble sort according to every pixel's value. Note that the largest end gets sorted first, with smaller elements taking longer to move to their correct positions.
Bubble sort. The list was plotted in a Cartesian coordinate system, with each point (x, y) indicating that the value y is stored at index x. Then the list would be sorted by bubble sort according to every pixel's value. Note that the largest end gets sorted first, with smaller elements taking longer to move to their correct positions.

Worked examples

Example 1 — a first encounter with Bubble sort

Start with the simplest possible case. Write down what Bubble sort claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bubble sort before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bubble sort ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bubble sort

In research
Bubble sort appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bubble sort in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bubble sort is common in secondary-school and first-year university syllabi. It links to neighbouring topics Comparison sorts, Stable sorts, so understanding it makes those chapters shorter.
In everyday life
Look for Bubble sort outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bubble sort” →

How to study Bubble sort in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bubble sort means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bubble sort out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bubble sort in simple terms?

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed. These passes through the list are repeated until no swaps have to…

Why does Bubble sort matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bubble sort?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bubble sort.

Tags

  • Comparison sorts
  • Stable sorts

Keep exploring