# C Program for Bubble Sort

In this tutorial, i am going to show you how to write a program for bubble sort with the help of for loop, while loop and function in c.

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. Example: First Pass: ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4.

## All C Program for Bubble Sort

• C Program for Bubble Sort using For Loop
• C Program for Bubble Sort using While Loop
• C Program for Bubble Sort using Function

### C Program for Bubble Sort using For Loop

```/* C Program for Bubble Sort */
#include <stdio.h>
int main()
{
int a[100], number, i, j, temp;

printf("\n Please Enter the total Number of Elements  :  ");
scanf("%d", &number);

printf("\n Please Enter the Array Elements  :  ");
for(i = 0; i < number; i++)
scanf("%d", &a[i]);
for(i = 0; i < number - 1; i++)
{
for(j = 0; j < number - i - 1; j++)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("\n List Sorted in Ascending Order : ");
for(i = 0; i < number; i++)
{
printf(" %d \t", a[i]);
}
printf("\n");
return 0;
}```

The result of the above c program; is as follows:

```Please Enter the total Number of Elements  :  5
Please Enter the Array Elements  :  9 4 1 8 6
List Sorted in Ascending Order :  1 	 4 	 6 	 8 	 9 ```

### C Program for Bubble Sort using While Loop

```#include <stdio.h>
int main()
{
int a[100], number, i, j, temp;

printf("\n Please Enter the total Number of Elements  :  ");
scanf("%d", &number);

printf("\n Please Enter the Array Elements  :  ");
for(i = 0; i < number; i++)
scanf("%d", &a[i]);
i = 0;
while(i < number - 1) {
j = 0;
while(j < number - i - 1) {
if(a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
j++;
}
i++;
}
printf("\n List in Ascending Order : ");
for(i = 0; i < number; i++)
printf(" %d \t", a[i]);
printf("\n");
return 0;
}```

The result of the above c program; is as follows:

```Please Enter the total Number of Elements  :  5
Please Enter the Array Elements  :  44 22 1 66 8
List in Ascending Order :  1 	 8 	 22 	 44 	 66 ```

### C Program for Bubble Sort using Function

```#include <stdio.h>
void bubFunc(int a[], int number) {
int i, j, temp;
for(i = 0; i < number - 1; i++) {
for(j = 0; j < number - i - 1; j++) {
if(a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
int main()
{
int arr[100], size, i;

printf("\n Please Enter the total Elements  :  ");
scanf("%d", &size);

printf("\n Please Enter the Array Elements  :  ");
for(i = 0; i < size; i++)
scanf("%d", &arr[i]);

bubFunc(arr, size);
printf("\n List in Ascending Order : ");
for(i = 0; i < size; i++)
{
printf(" %d \t", arr[i]);
}
printf("\n");
return 0;
}```

The result of the above c program; is as follows:

```Please Enter the total Elements  :  5
Please Enter the Array Elements  :  5 6 7 1 2
List in Ascending Order :  1 	 2 	 5 	 6 	 7 	```