Bubble Sort Algorithm Explained for Beginners
When beginners start learning sorting algorithms, Bubble Sort is usually one of the first algorithms they encounter. I remember learning it for the first time because it was simple enough to visualize step by step.
In this article, I will explain Bubble Sort in a beginner-friendly way with diagrams, examples, and a complete C program. By the end, you will clearly understand how Bubble Sort works internally.
What is Bubble Sort?
Bubble Sort is a simple sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order.
Real-Life Example of Bubble Sort
Imagine arranging students in ascending order based on height. You compare two students standing next to each other and swap them if they are in the wrong order. Repeating this process eventually sorts everyone correctly.
How Bubble Sort Works
Suppose we have the following array:
5, 1, 4, 2, 8
First Pass
[1, 5, 4, 2, 8]
Compare 5 and 4 → Swap
[1, 4, 5, 2, 8]
Compare 5 and 2 → Swap
[1, 4, 2, 5, 8]
After the first pass, the largest element reaches the end.
Second Pass
Compare 4 and 2 → Swap
[1, 2, 4, 5, 8]
The process continues until the entire array becomes sorted.
Bubble Sort Visualization
[7, 3, 9, 1]
Pass 1:
[3, 7, 1, 9]
Pass 2:
[3, 1, 7, 9]
Pass 3:
[1, 3, 7, 9]
Bubble Sort Algorithm
for each pass
{
compare adjacent elements
if elements are in wrong order
{
swap them
}
}
Bubble Sort Program in C
#include
void bubbleSort(int arr[], int n)
{
int i, j, temp;
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - i - 1; j++)
{
if(arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int n)
{
for(int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = {5, 1, 4, 2, 8};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted Array:\n");
printArray(arr, n);
return 0;
}
Output
1 2 4 5 8
Time Complexity of Bubble Sort
| Case | Complexity |
|---|---|
| Best Case | O(n) |
| Average Case | O(n²) |
| Worst Case | O(n²) |
Advantages of Bubble Sort
- Easy to understand
- Simple to implement
- Good for beginners
Disadvantages of Bubble Sort
- Very slow for large datasets
- Performs many unnecessary comparisons
Bubble Sort vs Merge Sort
| Feature | Bubble Sort | Merge Sort |
|---|---|---|
| Speed | Slow | Fast |
| Complexity | O(n²) | O(n log n) |
| Memory Usage | Low | Higher |
Video Tutorial
Frequently Asked Questions
Why is Bubble Sort called Bubble Sort?
Larger elements move to the end after every pass, similar to bubbles rising upward.
What is the time complexity of Bubble Sort?
Bubble Sort has O(n²) average and worst-case complexity.
Is Bubble Sort good for beginners?
Yes. It is one of the easiest sorting algorithms to learn.
Final Thoughts
Bubble Sort may not be the fastest sorting algorithm, but it is an excellent starting point for understanding how sorting works. Once you understand Bubble Sort, learning advanced algorithms becomes easier.
Try running the program yourself and trace each swap step by step to strengthen your understanding.
Post a Comment