 Last Updated:

# Bubble sort in C ## Algorithm

Input: Input the size and elements of an array
output: Displays the array in sorted order

1. Start
2. Read the size of the array say n
3. i = 0
4. while ( i <n )
4.1 read a [ i ]
4.2 i++
5. for i = 0 to n-1
5.1 for j = 0 to n-i-1
5.1.1 if a [ j ] > a [ j +1 ]
5.1.1.1 temp = a [ j ]
5.1.1.2 a [ j ] = a [ j +1]
5.1.1.3 a [ j + 1] = temp
6. print "The sorted array is " a [ n ]
7. Stop

## C Program # include <stdio.h>
int main ( )

{
int n,temp;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a [n];
printf("Enter the elements of the array one by one \n");
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

//Sorting logic
for (int i = 0; i < n; i++) {
for (int j = 0; j < (n-i-1); j++) {
if ( a[j] > a [j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}}
}

//Printing the sorted array
printf("The sorted array is \n");
for (int i = 0; i < n; i++){
printf("%d\t", a[i]);
}
}

Note: This Program was created using visual studio code. The Program file can be downloaded by clicking here

## Output

Enter the size of the array: 5
Enter the elements of the array one by one
1
64
36
42
8
The sorted array is
1       8       36      42      64