Bubble sort

A simple sorting algorithm in the field of computer science. It repeatedly visits the element columns to be sorted, and compares two adjacent elements in turn. If the order (such as from large to small, and the first letter from Z to a) is wrong, it exchanges them. The work of visiting elements is repeated until no adjacent elements need to be exchanged, that is to say, the element column has been sorted.

The name of this algorithm comes from the fact that the smaller the elements will slowly “float” to the top of the sequence (ascending or descending) through exchange, just as the bubbles of carbon dioxide in carbonated drinks will eventually float to the top, so it is called “bubble sorting”.

## Basic thinking

Compare two adjacent numbers at a time, and bring the smaller (or larger) number to the front. Repeat this step until the maximum (or minimum) number comes last.

## Algorithm description

1. Compare two adjacent data. If the first is larger than the second, exchange two numbers

2. Do the same work for each adjacent number, so that the number from the first team to the last team is the maximum number.

3. For operations above all elements, except the last one.

4. Repeat steps 1-3 until the sequence is complete.

## Algorithm implementation

(for example, sorting from small to large)

```
# include
int main(void)
{
int i, j, t, n;
int a[100];
scanf("%d",&n);
getchar();
For (I = 0; IA [J + 1]) // comparison of two adjacent numbers
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
for(i=0;i
```

## algorithm analysis

The bubble sorting method is stable, because when two numbers are equal, the two numbers do not need to be exchanged, and the relative position of the two numbers will not be changed