All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is Bubble Sort (algorithm explanation)?

Post cover: What is Bubble Sort (algorithm explanation)?
This content has been automatically translated from Ukrainian.
Bubble Sort this is one of the simplest sorting algorithms. Its essence is to compare neighboring elements of the array and swap them if they are in the wrong order. Thus, the "heaviest" element gradually "floats" onto the end of the array like a bubble in water, hence the name of the algorithm.
How it works in simple words:
  • Let's take an array of numbers
  • We compare the first number with the second
  • If the first is more than the second, we change their places
  • Let's move on to the next pair and repeat
  • Repeat the entire process several times until the array is sorted
An example of implementation in Ruby:
def bubble_sort(array)
  n = array.length
  loop do
    swapped = false
    (n-1).times do |i|
      if array[i] > array[i+1]
        array[i], array[i+1] = array[i+1], array[i]
        swapped = true
      end
    end
    break unless swapped
  end
  array
end

numbers = [5, 2, 9, 1, 5, 6]
sorted_numbers = bubble_sort(numbers)
You will see this in the terminal:
Screenshot 2025-09-16 at 18.40.33.png
Code Explanation: 
  • swapped monitors whether there have been changes to the current pass
  • If there were no changes in one pass, the array is sorted and you can stop
  • array[i], array[i+1] = array[i+1], array[i] swaps two elements
Bubble Sort is easy to understand, but it is slow for large arrays, so faster sorting algorithms are often used for practical problems.

This post doesn't have any additions from the author yet.

Error 403 on the site: what it means and how to eliminate it
24 Jul 23:50

Error 403 on the site: what it means and how to eliminate it

meme code
meme code@memecode
What is vibe coding?
25 Jul 21:51

What is vibe coding?

meme code
meme code@memecode
What is a combinatorial explosion?
28 Jul 11:50

What is a combinatorial explosion?

meme code
meme code@memecode
What is a brain stack?
28 Jul 19:37

What is a brain stack?

meme code
meme code@memecode
What is integer overflow?
15 Aug 08:28

What is integer overflow?

meme code
meme code@memecode
What is HAR file (HTTP Archive)?
25 Aug 18:23

What is HAR file (HTTP Archive)?

meme code
meme code@memecode
What is exponential growth?
16 Sep 18:57

What is exponential growth?

meme code
meme code@memecode
What is factorial complexity?
16 Sep 19:03

What is factorial complexity?

meme code
meme code@memecode
What is NP-complexity?
16 Sep 19:31

What is NP-complexity?

meme code
meme code@memecode
Offset vs Cursor Pagination in Rails: What to Choose and Why
24 Sep 15:22

Offset vs Cursor Pagination in Rails: What to Choose and Why

meme code
meme code@memecode
What is Row Security in PostgreSQL and why is it Rails developers
04 Oct 19:06

What is Row Security in PostgreSQL and why is it Rails developers

meme code
meme code@memecode
What is ivar in Ruby /Rails?
19 Oct 20:12

What is ivar in Ruby /Rails?

meme code
meme code@memecode