Bubble Sort Code in C
If you are starting your journey into data structures and algorithms, the bubble sort code in c is often the first sorting technique you will encounter. While it might not be the fastest algorithm for massive datasets, its logic is the foundation for understanding how computers organise information.
What is Bubble Sort?
Bubble sort is a simple, comparison based algorithm. It works by repeatedly stepping through a list, comparing adjacent elements and swapping them if they are in the wrong order. This process repeats until the largest elements "bubble up" to their correct positions at the end of the list.
Bubble Sort Code in C Using For Loop
The most common way to implement this is by using nested loops. The outer loop tracks the number of passes, while the inner loop handles the individual comparisons.
#include <stdio.h>
int main() {
int data[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(data)/sizeof(data[0]);
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (data[j] > data[j+1]) {
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
}
}
printf("Sorted array: \n");
for (i = 0; i < n; i++)
printf("%d ", data[i]);
return 0;
}
Bubble Sort Program in C User Input
In real world scenarios, you often need to sort data provided by a user. Here is how you can write a bubble sort program in c user input style to make it interactive.
#include <stdio.h>
int main() {
int a[100], n, i, j, swap;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers: \n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j+1]) {
swap = a[j];
a[j] = a[j+1];
a[j+1] = swap;
}
}
}
printf("Sorted list in ascending order: \n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
Bubble Sort Code in C Without Function
Many students look for a bubble sort code in c without function calls to keep the logic contained entirely within the main block. The examples provided above demonstrate this approach, making it easier to follow the flow of variables without jumping between different sections of memory.
Comparing Bubble Sort with Other Algorithms
While bubble sort is great for learning, it is important to know about other fundamental sorting techniques like selection sort and insertion sort.
Selection Sort in C
Selection sort works by finding the minimum element from the unsorted part and putting it at the beginning. It generally performs fewer swaps than bubble sort.
Insertion Sort in C
Insertion sort builds the final sorted array one item at a time. It is much more efficient for small datasets or arrays that are already mostly sorted.
Bubble Sort Code in C with Example Walkthrough
Let us look at a bubble sort code in c with example values. If you have an array (5, 1, 4):
- First Pass: Compare 5 and 1. Since 5 > 1, swap them. Array becomes (1, 5, 4).
- Compare 5 and 4. Since 5 > 4, swap them. Array becomes (1, 4, 5).
- The largest number 5 is now at the end.
Conclusion
Mastering the bubble sort code in c is a vital milestone for any programmer. It teaches the importance of loops, conditional logic, and array manipulation. Once you are comfortable with this, you can move on to more advanced algorithms like Quick Sort or Merge Sort.

Comments
Post a Comment