← homeProgramming (Програмування)

What is Bubble Sort (algorithm explanation)?

Bubble Sort is a simple sorting algorithm that compares adjacent elements of an array and swaps them until all the numbers are in order. It is easy to implement in Ruby, suitable for learning and small arrays, but slo...

This content has been automatically translated from Ukrainian.
Bubble Sort is one of the simplest sorting algorithms. Its essence lies in comparing adjacent elements of the array and swapping them if they are in the wrong order. Thus, the "heaviest" element gradually "bubbles" to the end of the array, like a bubble in water, hence the name of the algorithm.
How it works in simple terms:
  • Take an array of numbers
  • Compare the first number with the second
  • If the first is greater than the second - swap them
  • Move to the next pair and repeat
  • Repeat the whole process several times until the array is sorted
Example 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)
Type something like this in the terminal:
Screenshot 2025-09-16 at 18.40.33.png
Explanation of the code:
  • swapped tracks whether there were changes in the current pass
  • If there were no changes in one pass - the array is sorted and we 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 tasks.

🔥 More posts

All posts
What is vibe coding?
Jul 25, '25 21:51

What is vibe coding?

Vibe coding is intuitive programming in tandem with AI: without strict rules, with music, inspira...

What is a brain stack?
Jul 28, '25 19:37

What is a brain stack?

Brain stack is a model that describes how the brain works in layers: from neurons to self-awarene...

What is integer overflow?
Programming (Програмування)Aug 15, '25 08:28

What is integer overflow?

Integer overflow is the overflow of an integer when the value exceeds the limit of the variable t...

What is a HAR file (HTTP Archive)?
Programming (Програмування)Aug 25, '25 18:23

What is a HAR file (HTTP Archive)?

HAR file (HTTP Archive) is a .har format that stores a log of the browser's interaction with the ...

What is exponential growth?
Sep 16, '25 18:57

What is exponential growth?

Exponential growth is a rapid increase in magnitude, where each subsequent step multiplies the re...

What is factorial complexity?
Programming (Програмування)Sep 16, '25 19:03

What is factorial complexity?

Factorial complexity is the rapid increase in the number of options, where for n elements the pos...

What is NP-complexity?
Programming (Програмування)Sep 16, '25 19:31

What is NP-complexity?

NP-completeness is a class of problems where finding a solution is extremely difficult, but verif...

What is ivar in Ruby / Rails?
Programming (Програмування)Oct 19, '25 20:12

What is ivar in Ruby / Rails?

ivar in Ruby is an instance variable, denoted by @. In Rails, it passes data from the controller ...