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
iloop 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:
icompares its value withj- 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
iis smaller thanj - 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] = tempSwapping means exchanging values.
The temp variable temporarily stores one value so it does not get lost during replacement.
Simple Example
Suppose:
i = 1
j = 3Now condition becomes:
1 < 3Condition is true.
So swapping starts.
Swapping Step-by-Step
Step 1
temp = 1Temp stores value 1.
Step 2
1 = 3Now the first position gets value 3.
Step 3
3 = tempNow 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
- LinkedIn: https://www.linkedin.com/in/raees-yaqoob-qazi-ryqs/
- Medium Blog: https://medium.com/@raeesyaqubqazi
- Blogspot: https://brillertechnologies.blogspot.com/
- Facebook Page: https://web.facebook.com/profile.php?id=61553548371216
- YouTube Channel: https://www.youtube.com/@RaeesQ.
- TikTok: https://www.tiktok.com/@mrryqs?_t=ZS-8y7t0fQfJKu&_r=1
Comments
Post a Comment