If you are learning C programming, Bubble Sort is usually one of the first sorting algorithms you will encounter. From preparing for semester exams, coding interviews, placements, or beginner programming exercises, understanding the bubble sort program in C is extremely important. It helps students learn how arrays, loops, comparisons, and swapping operations work internally. At Codegnan, Bubble Sort is one of the first sorting techniques taught to beginners because it builds strong programming fundamentals before students move to advanced algorithms like Merge Sort, Quick Sort, and Heap Sort.
In this article, you will learn:
- What Bubble Sort is
- Bubble Sort algorithm in C
- Flowchart explanation
- Bubble Sort program in C with output
- Optimized Bubble Sort
- Time complexity
- Advantages and disadvantages
- Bubble Sort vs Selection Sort
- Frequently asked interview questions
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. The largest element gradually moves toward the end of the array after every pass, similar to how bubbles rise to the surface of water. That is why it is called Bubble Sort.
For example:
Unsorted Array:
5 1 4 2 8
Sorted Array:
1 2 4 5 8
Bubble Sort is widely used for:
- Learning sorting algorithms
- Understanding swapping logic
- Practicing loops and arrays
- Building programming fundamentals
- Preparing for coding interviews
Codegnan Insight
Many beginners directly jump into advanced DSA topics without mastering sorting fundamentals. At Codegnan, mentors focus heavily on Bubble Sort and Selection Sort first because students who understand basic sorting logic usually learn advanced algorithms much faster later.
Bubble Sort Algorithm in C
The Bubble Sort algorithm works in multiple passes.
During every pass:
- Adjacent elements are compared
- Elements are swapped if they are in the wrong order
- The largest unsorted element moves to its correct position
Steps of Bubble Sort Algorithm
Step 1: Start
Step 2: Read array elements
Step 3: Repeat for i = 0 to n-1
Step 4: Repeat for j = 0 to n-i-1
Step 5: Compare arr[j] and arr[j+1]
Step 6: If arr[j] > arr[j+1], swap them
Step 7: End inner loop
Step 8: End outer loop
Step 9: Print sorted array
Step 10: Stop
This is the standard bubble sort algorithm in C taught in colleges and placement training programs.
Flowchart of Bubble Sort
The flowchart of Bubble Sort follows this process:
- Start
- Input array elements
- Compare adjacent elements
- Swap if needed
- Repeat passes
- Print sorted array
- Stop
A flowchart helps beginners visually understand how Bubble Sort performs comparisons and swapping operations internally.
Bubble Sort Program in C
Here is the standard bubble sort program in C for arranging numbers in ascending order.
#include <stdio.h>
int main() {
int arr[100], n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Bubble Sort Logic
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;
}
}
}
printf("Sorted array:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
This is one of the most commonly asked bubble sort interview programs for freshers.
Output
Enter number of elements: 5
Enter array elements:
64
34
25
12
22
Sorted array:
12 22 25 34 64
Explanation of Bubble Sort Program

Let us understand how the program works internally.
Step 1: Input Array
The user enters array elements.
Example:
64 34 25 12 22
Step 2: Outer Loop
The outer loop controls the number of passes.
for(i = 0; i < n - 1; i++)
Step 3: Inner Loop
The inner loop compares adjacent elements.
for(j = 0; j < n - i - 1; j++)
Step 4: Swapping
If the left element is greater than the right element, they are swapped.
if(arr[j] > arr[j + 1])
Step 5: Final Sorted Array
After all passes are completed, the array becomes sorted in ascending order.
Codegnan Insight
One common mistake students make while writing Bubble Sort is incorrect loop boundaries. At Codegnan coding sessions, mentors encourage students to dry-run sorting programs manually on paper first. This improves debugging skills significantly during interviews and coding tests.
Optimized Bubble Sort in C
The standard Bubble Sort continues checking even if the array is already sorted. An optimized Bubble Sort stops early if no swapping occurs.
Optimized Bubble Sort Program
#include <stdio.h>
int main() {
int arr[100], n, i, j, temp, swapped;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < n - 1; i++) {
swapped = 0;
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;
swapped = 1;
}
}
// Stop if already sorted
if(swapped == 0) {
break;
}
}
printf("Sorted array:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
This optimized approach reduces unnecessary comparisons and improves efficiency.
Bubble Sort Program in C Without Using Function
Many college assignments ask students to write a bubble sort program in C without using function calls. The examples shown above already perform sorting directly inside the main() function without using user-defined functions. This approach makes it easier for beginners to understand sorting logic clearly.
C Program for Bubble Sort in Descending Order
Bubble Sort can also arrange numbers in descending order.
Bubble Sort Program in Descending Order
#include <stdio.h>
int main() {
int arr[100], n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
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;
}
}
}
printf("Sorted array in descending order:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
This is a commonly asked c program for bubble sort in descending order in placement interviews and practical exams.
Time Complexity of Bubble Sort
The performance of Bubble Sort depends on the number of comparisons performed.
Best Case
When the array is already sorted:
O(n)O(n)O(n)
This occurs in optimized Bubble Sort.
Average Case
O(n2)O(n^2)O(n2)
Worst Case
O(n2)O(n^2)O(n2)
This happens when the array is sorted in reverse order.
Space Complexity
O(1)O(1)O(1)
Bubble Sort does not require additional memory allocation.
Advantages and Disadvantages of Bubble Sort

| Type | Point | Explanation |
|---|---|---|
| Advantage | Easy to Learn | Bubble Sort is extremely beginner-friendly. |
| Advantage | Simple Implementation | The code structure is straightforward to debug. |
| Advantage | Good for Small Datasets | Works reasonably well for small arrays. |
| Advantage | Helps Build Fundamentals | Students understand loops, arrays, and swapping logic more clearly. |
| Disadvantage | Slow for Large Datasets | Performance becomes poor for large arrays. |
| Disadvantage | High Time Complexity | Bubble Sort requires many comparisons and swaps. |
| Disadvantage | Not Efficient for Real Projects | Modern applications use faster algorithms like Quick Sort and Merge Sort. |
Applications of Bubble Sort
Bubble Sort is mainly used in:
- Academic learning
- Small datasets
- Coding practice
- Interview preparation
- Understanding sorting concepts
Even though industries rarely use Bubble Sort for large-scale systems, companies still ask it in interviews because it tests logical thinking.
Codegnan Insight
Students who master basic sorting algorithms like Bubble Sort usually perform much better in Data Structures and Algorithms interviews later. At Codegnan, trainers gradually move students from Bubble Sort to advanced concepts like recursion, divide-and-conquer sorting, and competitive coding problems.
Bubble Sort vs Selection Sort
| Feature | Bubble Sort | Selection Sort |
|---|---|---|
| Method | Swaps adjacent elements | Selects minimum element |
| Number of Swaps | More | Fewer |
| Time Complexity | O(n²) | O(n²) |
| Simplicity | Very easy | Slightly harder |
| Stability | Stable | Not stable |
Both algorithms are beginner-friendly and commonly taught in C programming courses.
Why Bubble Sort is Important for Interviews
Many freshers think Bubble Sort is too simple to matter. But interviewers ask Bubble Sort questions to test:
- Array understanding
- Loop concepts
- Swapping logic
- Time complexity analysis
- Problem-solving ability
If you can explain Bubble Sort confidently with examples and complexity analysis, it creates a strong first impression during technical interviews. At Codegnan, students regularly practice sorting algorithms, coding problems, and interview questions through live coding sessions and hands-on assignments that improve placement readiness.
Key Takeaway
Bubble Sort is one of the most important beginner-level sorting algorithms in C programming. It helps students understand how sorting works internally through comparisons and swapping operations. Although Bubble Sort is not the fastest sorting algorithm, it remains extremely valuable for:
- Building programming fundamentals
- Learning arrays and loops
- Improving logical thinking
- Preparing for coding interviews
- Understanding algorithm complexity
If you are starting your programming journey, mastering Bubble Sort is an excellent first step before learning advanced sorting algorithms.
FAQs on Bubble Sort Program in C
What is Bubble Sort in C programming?
Bubble Sort is a sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order.
What is the time complexity of Bubble Sort?
The worst-case and average-case time complexity of Bubble Sort is:
O(n2)O(n^2)O(n2)
Why is it called Bubble Sort?
It is called Bubble Sort because larger elements gradually move toward the end of the array like bubbles rising to the surface of water.
Is Bubble Sort stable?
Yes, Bubble Sort is a stable sorting algorithm because equal elements maintain their original order.
Is Bubble Sort used in real-world applications?
Bubble Sort is mostly used for educational purposes and small datasets. Faster algorithms are preferred in large-scale applications.
Can Bubble Sort sort numbers in descending order?
Yes. You only need to reverse the comparison condition inside the swapping logic.
Why do interviewers ask Bubble Sort questions?
Interviewers use Bubble Sort to evaluate:
- Logic building
- Loop understanding
- Array manipulation
- Time complexity concepts
- Basic programming skills
Which is better: Bubble Sort or Selection Sort?
Selection Sort performs fewer swaps, but Bubble Sort is usually easier for beginners to understand and implement.




