> For the complete documentation index, see [llms.txt](https://cathyatseneca.gitbook.io/data-structures-and-algorithms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cathyatseneca.gitbook.io/data-structures-and-algorithms/sorting/simple_sorts/bubble_sort.md).

# Bubble Sort

A bubble sort is one of the simplest sorts to write. The idea behind a bubble sort is to start at the beginning of the array and swap adjacent elements that are not in order. Repeat this n-1 times where n is the size of the array and the array will be sorted.

```c
void bubble(int array[],int sz){
    int i,j;
    int tmp;
    for(i=0;i<sz-1;i++){
        for(j=0;j<sz-i-1;j++){
            if(array[j] > array[j+1]){
                //swap arr[j] and arr[j+1]
                tmp=array[j];
                array[j]=array[j+1];
                array[j+1]=tmp;
            }
        }
    }
}
```

[Bubble sort animation: http://cathyatseneca.github.io/DSAnim/web/bubble.html](http://cathyatseneca.github.io/DSAnim/web/bubble.html)
