# Bubble Sort in C

**Bubble sort in C** is a simple sorting algorithm that steps through the list repeatedly, compares adjacent elements of array or list and swaps them if elements are in wrong order.

If an array or list has total n elements, then we need to repeat this process for n-1 times. That means number of passes will be n-1.

## Bubble Sort Algorithm

**Pass 1: **

arr[0] & arr[1] are compared, and swapped if arr[0] > arr[1]

arr[1] & arr[2] are compared, and swapped if arr[1] > arr[2]

arr[2] & arr[3] are compared, and swapped if arr[2] > arr[3] and so on…

At the end of pass 1, the largest element of the list is placed at the highest index of the list i.e. (arr[n]).

**Pass 2:**

arr[0] & arr[1] are compared, and swapped if arr[0] > arr[1]

arr[1] & arr[2] are compared, and swapped if arr[1] > arr[2]

arr[2] & arr[3] are compared, and swapped if arr[2] > arr[3] and so on…

At the end of Pass 2 the second largest element of the list is placed at the second-highest index of the list i.e. (arr[n-1]).

**Pass n-1:**

arr[0] & arr[1] are compared, and swapped if arr[0] > arr[1]

arr[1] & arr[2] are compared, and swapped if arr[1] > arr[2]

arr[2] & arr[3] are compared, and swapped if arr[2] > arr[3] and so on…

At the end of this pass. The smallest element of the list is placed at the first index of the list i.e. (arr[0]).

## Example of Bubble Sort in C

Take an array of numbers ” 6, 3, -5, 15 , -51 “, and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Four passes will be required.

**Array: 6, 3, -5, 15, -51**

**Pass 1**

( ** 6**,

**, -5, 15, -51) –> (**

*3***3**,

**, -5, 15,- 51), Here, algorithm compares the first two elements and Swap since 6>3**

*6*(3, ** 6**, –

**, 15, -51) –> (3, –**

*5***5**,

**, 15, -51), Swap since 6 > – 5**

*6*( 3, -5, ** 6**,

**, -51) –> (3, -5,**

*15***,**

*6**, -51)*

**15**( 3, -5, 6, ** 15**, –

**) –> (3, -5, 6, –**

*51**,*

**51***), Swap since 15> -51*

**15**The last element is the largest element.

**Pass 2**

(** 3**, –

**, 6, -51, 15) –> (-**

*5**,*

**5****, 6, -51, 15), Swap since 3 > -5**

*3*(-5, ** 3**,

**, -51, 15) –> (-5,**

*6***,**

*3**, -51, 15)*

**6**(-5, 3, ** 6**, –

**, 15) –> (-5, 3, –**

*51***,**

*51**6*, 15), Swap since 6 > -51

The second last element is the second largest element.

**Pass 3** :

(-** 5**,

**, -51, 6, 15) –> (-**

*3***,**

*5**, -51, 6, 15)*

**3**(-5, ** 3**, –

**, 6, 15) –> (-5, –**

*51**,*

**51***, 6, 15), Swap since 3 > -51*

**3**The third last element is the third largest element.

**Pass 4 :**

(-** 5**, –

**, 3, 6, 15) –> (-**

*51**, –*

**51***, 3, 6, 15) , Swap since -5 > -51*

**5**At last we can say that if smallest element is in the last position of array or list it will take n-1 passes to move it to the beginning.

So , in this case we need 4 passes to sort array of 5 elements.

### Bubble Sort Program in C

#include <stdio.h> #define MAXSIZE 50 void bubbleSort(int arr[], int n) { int i, j, temp; for(i = 0; i < n; i++) { for(j = 0; j < n-i-1; j++) { if( arr[j] > arr[j+1]) { // swap the elements temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } // print the sorted array printf("Sorted Array is: "); for(i = 0; i < n; i++) { printf("%d ", arr[i]); } } int main() { int arr[MAXSIZE], i, n, temp; printf("Welcome to https://bptutorials.com \n"); // Input from User the total numbers of elemnts printf("Enter the number of elements to be sorted: "); scanf("%d", &n); // input array elemen ony by one for(i = 0; i < n; i++) { printf("Enter element no. %d: ", i+1); scanf("%d", &arr[i]); } printf(" ---------------------------------\n"); printf("Input array is :"); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); // calling function bubbleSort bubbleSort(arr, n); return 0; }