## C Program For Bubble Sort Algorithm in Data Structure

Learn How To Sort Integer Array using Bubble Sort Algorithm in C Programming Language. It is important that we should **How A For Loop Works** before getting further with the C Program Code. Here’s a Bubble Sort C Program with Output in Linux Terminal and Explanation.

#### What is Bubble Sort Algorithm?

Bubble Sorting Algorithm scans a List and Exchanges the Adjacent Elements basically. It **Compares each Element** with its **Adjacent Element** and Swaps it if the First Element is Greater. In this Algorithm, after every pass or the loop, the largest element in the unsorted list will be be placed at its proper place.

#### Bubble Sort Algorithm Analysis

Bubble Sorting should not be used for Large Lists instead it performs best for Smaller Lists. Bubble Sort Algorithm is a **Stable Sort**. Since it requires only one Temporary variable, it is an** In-Place Sort**. Space Complexity is O(1).

**Data in Sorted Order**: Time Complexity = **O(n)**

**Data in Reverse Order**: Time Complexity = **O(n ^{2})**

#### C Program To Sort Arrays using Bubble Sort Algorithm

#include<stdio.h> int main() { int elements[20], limit; int i, j, swaps, temp; printf("\nEnter Array Limit:\t"); scanf("%d", &limit); printf("\nEnter %d Elements in Array\n", limit); for(i = 0; i < limit; i++) { scanf("%d", &elements[i]); } for(i = 0; i < limit - 1; i++) { swaps = 0; for(j = 0; j < limit - 1 - i; j++) { if(elements[j] > elements[j + 1]) { temp = elements[j]; elements[j] = elements[j + 1]; elements[j + 1] = temp; swaps++; } } if(swaps == 0) break; } printf("\nSorted Array\n"); for(i = 0; i < limit; i++) printf("%d\t", elements[i]); printf("\n"); return 0; }

#### Output

If you have any compilation errors or doubts in this Code To Sort Array using Bubble Sort C Program in Data Structures, let us know about in the Comment Section below.

Thanks for this algorithm. This is the best and easiest algorithm for Bubble Sort in C Programming. Cheers!