In this article you will be familiarize with the bubble sort that is a sorting technique available for C programming language.

## Sorting :

Sorting is defined as the process of arranging an array in ascending order or descending order or it is defined as rearranging elements of an array.

There are different types of sorting available for C language which are given as following :

**Internal sort**: When all the sorted data is stored in a memory. For example insertion sort.**External sort**: When all the sorted data is stored outside the memory. For example merge sort.

## Bubble sort :

Bubble sort is a type of internal sorting technique.

Bubble sorting is a simplest sorting technique in which a pair of adjacent elements are compared and they are swapped if the elements are not in order.

### Algorithm of bubble sorting :

Step 1. begin

Step 2. for all elements of list repeat steps from step 3 to step 4

Step 3. if list[i] > list[i+1]

Step 4. swap(list[i], list[i+1])

end if

end for

Step 5. return list

Step 6. end

### How bubble sort works ?

Let us consider an example of array that is arr[].

arr[] = {14,24,6,97,36,2}

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 14 | 24 | 6 | 97 | 36 | 2 |

Bubble sort starts with comparing first two elements in an array and check whether arr[1] > arr[0] or not i.e. 24 > 14 that is true.

Therefore no swapping occurs.

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 14 | 24 | 6 | 97 | 36 | 2 |

Next 6 is compared with 24, 6 < 24; therefore elements are swapped.

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 14 | 6 | 24 | 97 | 36 | 2 |

Then 24 is compared with 97, 97 > 24 ; therefore no swapping occurs.

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 14 | 6 | 24 | 97 | 36 | 2 |

Next 97 is compared with 36, 36 < 97 ; therefore elements are swapped.

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 14 | 6 | 24 | 36 | 97 | 2 |

Then 97 is compared with 2, 2 < 97 ; therefore elements are swapped.

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 14 | 6 | 24 | 36 | 2 | 97 |

14 and 6 are compared and 6 < 14; therefore elements are swapped.

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 6 | 14 | 24 | 36 | 2 | 97 |

Next 14 and 24 are compared and 24 > 14; therefore elements are not swapped

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 6 | 14 | 24 | 36 | 2 | 97 |

Then 24 and 36 are compared and 36 > 24 ; therefore elements are not swapped

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 6 | 14 | 24 | 36 | 2 | 97 |

Then 36 and 2 are compared and 2 < 36 ; therefore elements are swapped.

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 6 | 14 | 24 | 2 | 36 | 97 |

Next 6 and 14 are compared and 14 > 6 ; therefore elements are not swapped

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 6 | 14 | 24 | 2 | 36 | 97 |

Next 14 and 24 are compared and 24 > 14; therefore elements are not swapped

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 6 | 14 | 24 | 2 | 36 | 97 |

Then 24 and 2 are compared and 2 < 24 ; therefore elements are swapped.

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 6 | 14 | 2 | 24 | 36 | 97 |

Next 6 and 14 are compared and 14 > 6 ; therefore elements are not swapped

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 6 | 14 | 2 | 24 | 36 | 97 |

then 14 and 2 are compared and 2 < 14; therefore elements are swapped

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 6 | 2 | 14 | 24 | 36 | 97 |

Lastly 6 and 2 are compared and 2 < 6 ; therefore elements are swapped

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 2 | 6 | 14 | 24 | 36 | 97 |

Therefore, an array has been sorted using bubble sorting technique. Sorted array is given below

Index | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|

Value | 2 | 6 | 14 | 24 | 36 | 97 |

This is all about the bubble sort that is a sorting technique available for C programming language.

If you have any query please comment below.