Bubble Sort Algorithm Explained in Easy Way

 Today I’m going to discuss one of the most famous sorting techniques in programming called Bubble Sort. As a DevOps engineer, understanding algorithms is also important because automation and scripting become easier when your logic building is strong.

Bubble Sort is a simple algorithm that sorts numbers step by step by comparing values with each other and swapping them when needed.

Let’s understand it in a very easy way.

What is Bubble Sort?

Bubble Sort is a sorting technique that compares two values again and again and places the smaller value before the bigger value.

The process keeps repeating until the complete list becomes sorted.

For example:

[-2, -1, 0, 1, 1, 2, 3, 4, 5, 56, 89]

This is called ascending order because numbers are arranged from smaller to larger.

Bubble Sort Code Example

list_of_num = [1,1,2,3,-1,4,-2,0,56,89,5]
for i in range(len(list_of_num)):
for j in range(len(list_of_num)):
if list_of_num[i] < list_of_num[j]:
            temp = list_of_num[i]
list_of_num[i] = list_of_num[j]
list_of_num[j] = temp
print(list_of_num)

Step-by-Step Explanation

1. Creating the List

list_of_num = [1,1,2,3,-1,4,-2,0,56,89,5]

Here we create a list that contains positive numbers, negative numbers, and zero.

2. First Loop

for i in range(len(list_of_num)):
  • len(list_of_num) counts the total elements in the list.
  • range() creates the loop range.
  • The i loop starts checking elements one by one.

3. Second Loop

for j in range(len(list_of_num)):

This loop runs inside the first loop.

Now:

  • i compares its value with j
  • Every element gets checked with every other element

This is the main concept behind Bubble Sort.

4. Condition Checking

if list_of_num[i] < list_of_num[j]:

Here we check:

  • If the value of i is smaller than j
  • Then we swap both values

This helps arrange numbers in ascending order.

Understanding the Swapping Process

temp = list_of_num[i]
list_of_num[i] = list_of_num[j]
list_of_num[j] = temp

Swapping means exchanging values.

The temp variable temporarily stores one value so it does not get lost during replacement.

Simple Example

Suppose:

i = 1
j = 3

Now condition becomes:

1 < 3

Condition is true.

So swapping starts.

Swapping Step-by-Step

Step 1

temp = 1

Temp stores value 1.

Step 2

1 = 3

Now the first position gets value 3.

Step 3

3 = temp

Now second position gets value 1.

Values are successfully swapped.

Final Output

After all comparisons and swapping, the final sorted list becomes:

[-2, -1, 0, 1, 1, 2, 3, 4, 5, 56, 89]

Why Bubble Sort is Important?

Bubble Sort is important for beginners because it helps in understanding:

  • Loops
  • Conditions
  • Comparisons
  • Swapping logic
  • Problem-solving skills

Even though Bubble Sort is not the fastest sorting algorithm, it is one of the best techniques for learning programming fundamentals.

Conclusion

Bubble Sort is a simple and beginner-friendly sorting algorithm. It compares numbers one by one and swaps them until the list becomes fully sorted.

If you understand Bubble Sort clearly, learning advanced algorithms like:

  • Selection Sort
  • Merge Sort
  • Quick Sort

will become much easier.

🌐 Online References

Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator