C program for bubble sort is a simple and easy program implemented in c and mostly asked in interviews, exams etc.

**Bubble sort in c** is used for sorting element in particular order as ascending or descending.

**A logic for bubble sort**

C program for bubble sort

In this technique we compare first element with preceding element if the number next to that is small then we perform swapping so that the smaller number become a first, Then again go for next preceding number this process will continues till end of iteration and we perform N number of iteration to sorting element at last we will get sorted list.

Using this technique at first iteration we bubble up either the first or last element while arranging in ascending or descending order respectively.

**Time complexity for bubble sort** **worst case** **O(n ^{2})**.

**Advantages of bubble sort in c:-**

- Bubble sort is easy to implement.
- No extra space required while implementation.

**Disadvantages of bubble sort in c:-**

- It’s not possible to implement bubble sort if we have large number of array.
- Not suitable for real time application.

**Bubble sort algorithm in c ( bubble sort in c )**

- Start
- Take numbers input from user
- Traverse the array till end
- Compare i array index value with i+1 and bubble up large value to end
- print sorted array
- End

## Bubble sort program 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 28 29 30 31 32 33 34 |
#include <stdio.h> int main() { int array[100], n, c, d, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0 ; c < n - 1; c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } printf("Sorted list in ascending order:\n"); for (c = 0; c < n; c++) printf("%d\n", array[c]); return 0; } |

**Output :**

**Explanation :**

**1. Take number from user.**

=> n = 5

**2. Take n numbers from user.**

=> array[]= 10,4,78,4,9

**3. Traverse the array from start to end. **

compare i and i+1 value if i value is grater than swap the i and i+1 values else continue.

**4. Pritnt sorted array.**

## Leave a Reply