# Bubble sort program in C using array with examples

There are many techniques to sort the values. **Bubble sort** is one of the sorting algorithm used to sort the values in effective way.Bubble sort is compare the values of adjacent elements repeatedly and then swapping their position if they exist in the wrong order.

**Bubble sort Algorithm : How it works**

1.The unsorted array of 5 elements given as input to bubble sort.It will compare the first and second element to check which one is greater and swap the elements if first element is greater than the second element.

2.Compare the second and third element to check which one is greater and swap the element if the second element is greater than third element.

3.Compare the third and fourth element to check which one is greater and swap the element if the third element is greater than fourth element.

4.Compare the fourth and fifth element to check which one is greater and swap the element if the fourth element is greater than fifth element.

5.Repeat steps 1-5 until no more swaps are required.

**Example of Bubble sort**

**Input values in unsorted order:**

5

4

3

2

1

**Implementation:**

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 28 29 30 31 32 33 34 |
#include<stdio.h> int main() { int a[100],i,j,n,temp; printf("Enter the 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]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } printf("The sorted elements are: \n"); for(i=0;i<n;i++) { printf("%d\n",a[i]); } } |