# 冒泡排序 Bubble sort

## Bubble sort冒泡排序

@See <https://en.wikipedia.org/wiki/Bubble_sort> @See <https://github.com/jiek2529/java_algorithm> - BubbleSort

![](https://2531019738-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6gnm_fO-nBQv_cernA%2Fsync%2F1b3371cf9746c231c804b1b7d3f8a129f81b2b1c.gif?generation=1588815965699030\&alt=media)

### principle原理

依次两两比较，把大数向后置换，最后数在最后，最小数在最前。 每轮排完，下轮排序个数减一。

## example示例

```java
public void sort(int[] list) {
        for (int i = list.length - 1; i > 0; i--) {//循环列表长减一次。
            for (int j = 0; j < i; j++) {//从最左向右比较交换
                if (list[j + 1] < list[j]) {
                    swap(list, j, j + 1);
                }
            }
        }
    }
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jiek.gitbook.io/sorting-algorithm/liu-hang-pai-xu-suan-fa-popular-sorting-algorithms/mao-pao-pai-xu-yu-bian-zhong-bubble-sort-and-variants/bubble_sort.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
