Contact Information

2780. So Jones Blvd
Las Vegas, Nevada 89146
USA

We Are Available 24/ 7. Call Now.

Bubble Sort in C provides the opportunity to arrange numbers in ascending order and descending order, as well as sort strings in programming. Bubble sort is a sorting algorithm in C that checks and swaps elements in an intended order of events. Bubble sort compares adjacent elements to find which is greater or lesser until the element finds its final place.

Java Programming for Complete Beginners Learn in 250 Steps

Last Updated: 2022-04-22
4.8 (34855 views)

Start Learning Java Programming Step By Step with 200+ code examples. 250 Amazing Steps For Absolute Java Beginners!

How does Bubble Sort work?

In the bubble sort, the smaller elements bubble to the top of the list, while the larger elements settle to the bottom. Here is a step-by-step guide on the working of bubble sort:

  1. Compare the first two elements of the list. If the first element is larger, then swap it with the second element.
  2. Move to the next pair of elements, compare them, and swap them if required.
  3. Continue till the list is finished.
  4. If swaps are made in the previous step, repeat them from the beginning.
  5. Continue this process till the entire list is sorted.

Here is an example of how to code to use bubble sort in C programming:

function bubbleSort(arr):
n = length(arr)
do
swapped = false
for i = 0 to n-2 do
if arr[i] > arr[i+1] then
swap(arr[i], arr[i+1])
swapped = true
end for
n = n – 1
while swapped is true
end function

The swapped variable tracks the number of swaps made throughout the list. The swaps continue till the time list is sorted and the algorithm stops. Throughout the outer loop, length decreases as the largest element sinks to the bottom.

How does the C program for bubble sort work?

The C program sorts and swaps adjacent elements after comparing them in an array. An array with ‘n’ elements, if you want to sort in ascending order, this is how the bubble sort algorithm works:

  1. Start from the index 0, and compare the first and second elements.
  2. If element 1 is greater than the second, it is swapped.
  3. This process continues till the list is sorted in an ascending manner, i.e, n-1.

All steps are repeated for each iteration, and after the iteration, the largest unsorted element will be shifted to the end of the array.

Here are two examples of bubble sort in C programming:

  1. Bubble Sort in C Program using For Loop:

#include

int main() {
int arr[100], n, i, j, temp;

printf(“Enter the number of elements in the array: “);
scanf(“%d”, &n);

printf(“Enter %d integers:\n”, 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 list in ascending order:\n”);

for (i = 0; i < n; i++) {
printf(“%d\n”, arr[i]);
}

return 0;
}

At outer loop- First, the user mentions the number of elements they want to sort, using for loop to input the elements in a desirable manner. Then, for loops perform the bubble sort, from 0th elements to n-1th element.
At the inner loop- to compare adjacent elements in the array, we swap if the n+1 element is bigger than the n element, therefore bubbling up the larger element to the end of the array.

  1. Bubble Sort in C Program using While Loop:

#include

int main() {
int arr[100], n, i, j, temp;

int swapped;

printf(“Enter the number of elements in the array: “);
scanf(“%d”, &n);

printf(“Enter %d integers:\n”, n);

for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}

i = 0;
swapped = 1;

while (swapped) {
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;

}
}
i++;
}

printf(“Sorted list in ascending order:\n”);

for (i = 0; i < n; i++) {
printf(“%d\n”, arr[i]);
}

return 0;
}

At outer loop- First, the user mentions the number of elements they want to sort, using for loop to input the elements in a desirable manner. Variable ‘i’ to 0 and the variable is swapped to 1, and this swapped variable tracks the swaps in the iteration. Then, for loops perform the bubble sort, from 0th elements to n-1th element.
At the inner loop- we increase i, till the ‘swapped’ variable is not equal to 0, which states at least one swap was made in the iteration.

Top Courses in Coding & Developer
Share:

A recent graduate in Political Science from Lady Shri Ram College for Women, Ashli is a published researcher and a poet. She is currently working as a technical writer at Learnfly. She is a huge admirer of British thespian, American sitcoms and crime thrillers in languages with subtitles. She loves exploring art in all forms and is equipped with learning coding and video-editing skills, in her free time.