Here you will learn about program for bubble sort in C.

Bubble sort is a simple sorting algorithm in which each element is compared with adjacent element and swapped if their position is incorrect. It is named as bubble sort because same as like bubbles the lighter elements come up and heavier elements settle down.

Both worst case and average case complexity is O (n^{2}).

## Bubble Sort in C

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include<stdio.h> int main() { int a[50],n,i,j,temp; printf("Enter the size of array: "); scanf("%d",&n); printf("Enter the array elements: "); for(i=0;i<n;++i) scanf("%d",&a[i]); for(i=1;i<n;++i) for(j=0;j<(n-i);++j) if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } printf("\nArray after sorting: "); for(i=0;i<n;++i) printf("%d ",a[i]); return 0; } |

**Output**

*Enter the size of array: 4*

*Enter the array elements: 3 7 9 2*

*Array after sorting: 2 3 7 9*

Comment below if you have any doubts related to above program for bubble sort in C.

sir, i want to know what ide you use? can you send me link to down load it

I use codeblocks, download it from link given below:

https://www.thecrazyprogrammer.com/2013/06/gcc-compiler-download-codeblocks-1211.html

i++ and j++ can be used instead of ++i and ++j…?

Yes you can use, both will work same in this case.

Sir can I get algorithm for this program?