
The first one that comes to your mind when you start learning about sorting algorithms is Bubble Sort. Why? Because it is simple and helps understand the basic logic of an algorithm. Let’s learn the bubble sort algorithm in C, no worry, we’ll keep it light and entertaining. By the end, you will understand why people, sometimes hate it because of its inefficiency, and you’ll get a very clear picture about how to implement it in C.
What is Bubble Sort?
Bubble Sort is a simple sorting algorithm that works by repeatedly comparing adjacent elements in an array and swapping them if they are in the wrong order. The process continues until the largest unsorted element “bubbles” reach its correct position, much like air bubbles rising to the surface of water.
Suppose you are arranging a deck of playing cards in hand. You compare two cards hand to hand, and if the card on the left is larger than the card on the right, you swap them. Repeat this process until your whole hand is sorted.
But let’s get down to the nitty-gritty details:
- It works by repeatedly stepping through a list.
- Adjacent elements are compared and swapped if they are in the wrong order.
- This is repeated until no more swaps are needed that’s when the list is sorted.
Sounds so simple, right? Well, it is. However, once you are dealing with large datasets, it tends to become slow.
How Bubble Sort Works
Example scenario:
Imagine sorting these numbers: [5, 3, 8, 4].
- Pass 1: Compare 5 & 3 → Swap → [3, 5, 8, 4]
Compare 5 & 8 → No swap → [3, 5, 8, 4]
Compare 8 & 4 → Swap → [3, 5, 4, 8] - Pass 2: Compare 3 & 5 → No swap → [3, 5, 4, 8]
Compare 5 & 4 → Swap → [3, 4, 5, 8] - Pass 3: Compare 3 & 4 → No swap → [3, 4, 5, 8]
Result: [3, 4, 5, 8]
Key Characteristics of Bubble Sort
- Stability:
Bubble Sort is a stable algorithm. This means that if two elements have the same value, their original order is preserved. Stability is crucial when sorting data with multiple attributes, like a list of students sorted by name and then by age. - Comparison with other sorting algorithms:
- Insertion Sort: Similar simplicity but often more efficient for small datasets.
- Selection Sort: Better suited for cases where minimizing swaps is crucial.
- Quick Sort: This is far more efficient for large datasets but is more complex to implement.
Bubble Sort may not win the “speed race,” but it remains a classic algorithm for understanding the building blocks of sorting.
Why Are Sorting Algorithms Important in Programming?
Sorting algorithms like Bubble Sort play a vital role in many real-world applications. They’re used in tasks such as:
- Organizing data for quick searches (e.g., phonebooks, product catalogs).
- Optimizing algorithms that depend on sorted input (e.g., binary search).
- Simplifying complex data manipulations, like merging datasets.
Though Bubble Sort is not the most efficient sorting method, it can help one understand more advanced algorithms.
Why Learn Bubble Sort in C?
- C is foundational: As one of the first programming languages, C is widely used for teaching fundamental algorithms due to its close-to-hardware nature and high efficiency.
- Direct memory manipulation: Bubble Sort in C helps you understand how arrays and loops interact with memory.
- Strong algorithmic thinking: Learning Bubble Sort in C builds a solid foundation for tackling more complex algorithms in future programming challenges.
Algorithm for Bubble Sort in C
Step-by-Step Explanation

Here’s a simple breakdown of the Bubble Sort process:
- Start with an unsorted array of numbers.
- Compare the first two elements:
- If the first is larger, swap them.
- If they’re in the correct order, do nothing.
- Move to the next pair and repeat the process until the last element.
- After the first pass, the largest element will be in its correct position.
- Repeat the process for the remaining unsorted elements, ignoring the sorted ones.
- Continue until the entire array is sorted.
Pseudocode for Bubble Sort:
function BubbleSort(arr):
n = length(arr)
for i from 0 to n-1:
swapped = false
for j from 0 to n-i-2:
if arr[j] > arr[j+1]:
swap(arr[j], arr[j+1])
swapped = true
if swapped == false:
break
Step-by-Step Description:
- Outer Loop: Runs for the number of passes required to sort the array.
- Inner Loop: Compares and swaps adjacent elements if necessary.
- Swapped Flag: Tracks if a swap occurs during a pass. If no swaps are made, the array is already sorted.
- Optimization: The inner loop shrinks after each pass because the largest elements are already sorted.
Flowchart of Bubble Sort
A flowchart visually simplifies Bubble Sort. It starts with initializing the array, iterates through comparisons and swaps, and checks if the array is sorted.
Flowchart:
- Start
- Input array
arr
- Initialize
i = 0
- Check:
i < n - 1
- Yes: Proceed to the inner loop.
- No: End.
- Compare
arr[j]
andarr[j+1]
- If
arr[j] > arr[j+1]
: Swap - Increment
j
. Repeat the inner loop until the end. - Increment
i
. Repeat the outer loop.
Bubble Sort C Code Implementation
Here’s the C code for that:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap adjacent elements
int 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]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array:\n");
printArray(arr, n);
bubbleSort(arr, n);
printf("Sorted array:\n");
printArray(arr, n);
return 0;
}
Output:
Unsorted array:
64 34 25 12 22 11 90
Sorted array:
11 12 22 25 34 64 90
Optimized Code
We can reduce unnecessary iterations by stopping the algorithm early if no swaps are made during a pass.
#include <stdio.h>
#include <stdbool.h>
void optimizedBubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
bool swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap adjacent elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// Early exit if no swaps occurred
if (!swapped) break;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array:\n");
printArray(arr, n);
optimizedBubbleSort(arr, n);
printf("Sorted array:\n");
printArray(arr, n);
return 0;
}
Explanation of Early Exit Condition:
The swapped
flag ensures that the algorithm terminates early if the array becomes sorted before all passes are completed. This reduces unnecessary comparisons, improving efficiency.
Real-World Analogy:
Imagine sorting books on a shelf. If a pass doesn’t require any swaps, you know the books are already in order, so you stop. This saves time and effort.
Pro Tip: Always start with the basic implementation to understand the logic. Then, use optimizations to enhance performance for larger datasets.
Time Complexity of Bubble Sort
The time complexity depends on the nature of the input array. Here’s an analysis:

Best Case Time Complexity of Bubble Sort (O(n)):
When the array is already sorted, the algorithm completes in one pass without any swaps.
Example Input:[1, 2, 3, 4, 5]
Output:[1, 2, 3, 4, 5]
Explanation:
No swaps are needed, so the swapped
flag remains false
after the first pass, and the algorithm exits early.
Average Case Time Complexity of Bubble Sort (O(n²)):
When the elements are in random order, the algorithm compares and swaps elements for each pass.
Example Input:[3, 1, 4, 5, 2]
Output:[1, 2, 3, 4, 5]
Explanation:
Each element requires multiple comparisons and swaps to reach its correct position. The inner and outer loops contribute to the quadratic complexity.
Worst Case Time Complexity of Bubble Sort (O(n²)):
When the array is sorted in reverse order, It makes the maximum number of comparisons and swaps.
Example Input:[5, 4, 3, 2, 1]
Output:[1, 2, 3, 4, 5]
Explanation:
The algorithm compares every pair of adjacent elements and performs swaps for each.
Detailed Examples with Input and Output
Input Array | Scenario | Number of Comparisons | Number of Swaps | Time Complexity |
---|---|---|---|---|
[1, 2, 3, 4, 5] | Already sorted | n−1 | 0 | O(n) |
[3, 1, 4, 5, 2] | Random order | n2/2 | ~n2/4 | O(n2) |
[5, 4, 3, 2, 1] | Reverse sorted | n2/2 | n2/2 | O(n2) |
Space Complexity of Bubble Sort

Bubble Sort is an in-place sorting algorithm, meaning it sorts the array by modifying the original data structure without requiring additional memory.
Space Complexity: O(1).
In-Place Sorting Explanation
- All swaps happen directly within the array, avoiding the need for auxiliary storage.
- Only a constant amount of extra space (temporary variable for swapping) is used.
Advantages and Limitations
Advantages of Bubble Sort
- Simplicity: Easy to implement and understand. Ideal for teaching basic algorithmic concepts.
- In-Place Algorithm: Requires no additional memory, making it suitable for memory-constrained environments.
- Best Case Optimization: Efficient when the input is already sorted, requiring only O(n) time.
Limitations of Bubble Sort
- Inefficiency: Poor performance on large datasets due to its O(n2) time complexity in most scenarios.
- Limited Practical Use: Not suitable for sorting large or complex datasets compared to other algorithms like Quick Sort or Merge Sort.
Applications of Bubble Sort
Real-World Use Cases

- Small Datasets:
It performs well for small or nearly sorted datasets where its overhead is minimal. Example:- Sorting a small list of items like exam scores.
- Rearranging a few out-of-order elements in a nearly sorted list.
- Educational Purposes:
Used in programming courses to teach sorting fundamentals and algorithm design.
Example Applications in Daily Programming Tasks
- Sorting Inputs in a Small Program:
Quickly arranging user inputs in ascending or descending order for short-lived applications. - Debugging or Learning Algorithms:
It helps beginners grasp concepts like nested loops, conditional statements, and in-place operations.
Niche Applications in Real-world Tools
While Bubble Sort isn’t a go-to choice for production systems, it occasionally appears in niche applications where small datasets or simplicity is key.
- Embedded Systems: In environments with limited memory and computational power, such as microcontrollers, Bubble Sort might be employed for sorting small arrays.
- Game Development: Certain game engines use Bubble Sort for sorting small lists, like arranging objects in a specific order within a game scene.
Learning Benefits for Beginners
- Understand Basic Concepts: Teaches foundational algorithm design and control flow.
- Hands-On with Nested Loops: Demonstrates how nested iterations work in real-world problems.
- Step-by-Step Debugging: Encourages students to analyze each step, improving debugging skills.
- Transition to Advanced Algorithms: Serves as a stepping stone to learn more efficient sorting techniques.
Is Bubble Sort Efficient? (Spoiler: Not Really!)
Okay, here’s the truth: Bubble Sort isn’t the most efficient sorting algorithm out there. In fact, for large datasets, it’s one of the slowest, with a time complexity of O(n²). That’s quadratic time, meaning as the dataset grows, the time taken increases exponentially. Not great for big data.
But why do people still learn it? Because it’s simple and works well for small datasets or as a teaching tool to understand sorting logic.
In fact, real-world sorting tasks—like organizing millions of items on e-commerce websites—don’t rely on Bubble Sort. They use much faster algorithms like quick sort or merge sort (which have time complexities of O(n log n)).
Comparing Bubble Sort with Other Sorting Algorithms

Key Differences between Insertion Sort vs Bubble Sort
Aspect | Bubble Sort | Insertion Sort |
---|---|---|
Working Mechanism | Compares and swaps adjacent elements. | Inserts elements into their correct position. |
Best Case Time Complexity | O(n) (already sorted array). | O(n) (already sorted array). |
Average/Worst Case | O(n2). | O(n2). |
Number of Comparisons | More comparisons even in sorted data. | Comparisons are reduced when elements are sorted. |
Use Case | Better for small, unsorted datasets. | Suitable for datasets that are nearly sorted. |
Both are straightforward to code, but Insertion Sort is often preferred due to its efficiency on partially sorted arrays.
Key Differences between Selection Sort vs Bubble Sort
Aspect | Bubble Sort | Selection Sort |
---|---|---|
Working Mechanism | Repeatedly swaps adjacent elements to sort. | Selects the smallest element and swaps it into position. |
Number of Swaps | Potentially high due to adjacent swaps. | Fewer swaps since it finds the correct element in each iteration. |
Time Complexity | O(n2) for all cases. | O(n2) for all cases. |
Stability | Stable (maintains relative order of equal elements). | Unstable (relative order may change). |
Use Case | Useful for demonstrating sorting logic. | Efficient for small datasets requiring fewer swaps. |
Selection Sort is preferred when minimizing swaps is critical, such as in scenarios with memory constraints.
Common Mistakes and Debugging Tips
Common Errors in Implementation
1. Off-by-One Errors:
- Mistake: Incorrectly setting loop boundaries, leading to incomplete passes or array out-of-bounds errors.
- Fix: Ensure the outer loop runs n−1 times, and the inner loop decreases in range with each pass.
2. Incorrect Swap Logic:
- Mistake: Forgetting to use a temporary variable when swapping elements, leading to data overwriting.
- Fix: Always use a temporary variable to facilitate the swap.
Incorrect Code Example:
arr[j] = arr[j+1];
arr[j+1] = arr[j]; // Overwrites data.
Correct Code Example:
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
3. Misplaced Break Condition:
- Mistake: Placing the break condition outside the inner loop, causes the algorithm to run unnecessary iterations.
- Fix: Use the
break
condition inside the inner loop and check theswapped
flag.
Debugging Strategies
How can I identify errors in my Bubble Sort code?
1. Print Debug Statements:
Add print statements to monitor array changes after each iteration:
printf("Pass %d: ", i);
for (int k = 0; k < n; k++) {
printf("%d ", arr[k]);
}
printf("\n");
2. Dry Run the Code:
Walk through the algorithm with a small example (e.g., [4, 3, 2, 1]
) to manually verify each step.
3. Test Edge Cases:
Validate the implementation with:
- Already sorted arrays.
- Reverse-sorted arrays.
- Single-element arrays.
- Empty arrays.
4. Check Loop Boundaries:
Ensure the inner loop’s range adjusts correctly as the algorithm progresses.
Conclusion
Recap of the Algorithm and Its Significance
Bubble Sort is a simple and intuitive sorting algorithm that compares adjacent elements and swaps them to achieve a sorted order. While it is not efficient for large datasets due to its O(n2) complexity, it serves as an excellent starting point for understanding fundamental algorithmic principles.
Key Takeaways:
- Time Complexity: Best case O(n), Worst case O(n2).
- Space Complexity: O(1), as it sorts in place.
- Strengths: Simplicity, stability, and utility for small datasets.
- Weaknesses: Inefficiency with large or complex datasets.
Encouragement to Practice and Experiment
To truly understand:
- Practice coding it from scratch.
- Experiment with variations, such as optimizing with an early exit condition.
- Compare it with other algorithms to appreciate its strengths and weaknesses.
It is more than just a sorting algorithm; it’s a gateway to mastering algorithmic thinking and problem-solving!
FAQs
Bubble Sort gets its name from the way the largest element “bubbles up” to its correct position in each pass through the array. This process resembles bubbles rising to the surface of the water, with each pass gradually placing the next-largest element in order.
No, Bubble Sort is not suitable for large datasets due to its O(n2) time complexity. As the input size grows, the number of comparisons and swaps increases significantly, making it inefficient compared to more advanced sorting algorithms like Quick Sort or Merge Sort.
Bubble Sort is simple to understand and implement, making it an excellent starting point for beginners learning sorting algorithms. Additionally, it works well for small datasets or nearly sorted arrays due to its ease of implementation and low overhead.
For an already sorted array, Bubble Sort can complete the sorting process in O(n) time. With the optimized version, the algorithm detects no swaps during the first pass and exits early, avoiding unnecessary iterations.
Bubble Sort is intuitive and visually straightforward, making it easy for beginners to grasp the concepts of comparison and swapping. Its step-by-step approach also helps learners understand the basics of algorithm design and complexity analysis.
Additional Resources
Links to Coding Platforms for Practice
- HackerRank
Solve real-world sorting problems and test your skills. - LeetCode: Sorting Algorithms
A variety of sorting-related challenges for all difficulty levels.
Recommended Books and Tutorials on Algorithms
- Books
- Introduction to Algorithms by Thomas H. Cormen et al.
A comprehensive guide to all major algorithms. - Algorithms by Robert Sedgewick and Kevin Wayne.
Covers sorting algorithms with detailed visualizations and code examples.
- Introduction to Algorithms by Thomas H. Cormen et al.
- Online Tutorials
- Khan Academy: Intro to Sorting
Beginner-friendly videos and exercises. - MIT OpenCourseWare: Algorithms
In-depth video lectures on algorithms, including sorting.
- Khan Academy: Intro to Sorting
- Interactive Learning
- VisuAlgo: Sorting Visualization
Watch sorting algorithms in action with step-by-step animations.
- VisuAlgo: Sorting Visualization
These resources will help you dive deeper into the world of sorting algorithms, solidify your understanding of Bubble Sort, and build a strong foundation in algorithm design.