# Bubble Sort

Bubble sort is an algorithm to sort elements stored in an data structure like array. Bubble Sort compares all the element one by one and sort them based on their values.

It is called Bubble sort, because with each iteration the largest element in the list bubbles up towards the last place, just like a water bubble rises up to the water surface.

Sorting takes place by comparing adjacent data items one by one and swapping each pair that is out of order. Bubble Sort 1st Iteration

Above figure shows one iteration of algorithm in which largest element 93 is bubbled up and placed at last position. Bubble sort require N-1 iterations to completely sort an array. So here after N-1 = 8 iterations, array will be sorted.

–> Complexity of Bubble Sort is O(n²)

##### C Program to Implement Bubble Sort algorithm
```#include
#include

void bubble_sort(int[], int);

void main()
{
int arr, num, i;

printf("\nEnter no of elements :");
scanf("%d", &num);

printf("\nEnter array elements :");
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);

bubble_sort(arr, num);
getch();
}

void bubble_sort(int iarr[], int num)
{
int i, j, k, temp;

printf("\nUnsorted Data:");
for (k = 0; k < num; k++)
{
printf("%5d", iarr[k]);
}

for (i = 1; i < num; i++)
{
for (j = 0; j < num - 1; j++) { if (iarr[j] > iarr[j + 1])
{
temp = iarr[j];
iarr[j] = iarr[j + 1];
iarr[j + 1] = temp;
}
}

printf("\nAfter pass %d : ", i);
for (k = 0; k < num; k++)
{
printf("%5d", iarr[k]);
}
}
}```

OUTPUT: Bubble Sort Output