# 2. 冒泡排序

一句话总结：

> 第一轮把最大的冒泡到最后一个位置
>
> 第二轮把第二大的冒泡到倒数第二个位置
>
> ....

## java

```java
public abstract class Sort<T extends Comparable<T>> {
    public abstract void sort(T[] nums);

    public boolean less(T v,T w){
        return v.compareTo(w)<0;
    }

    public void swap(T[] a,int i,int j){
        T t=a[i];
        a[i]=a[j];
        a[j]=t;
    }
}


public class Buddle<T extends Comparable<T>> extends Sort<T> {
    @Override
    public void sort(T[] nums) {
        int n=nums.length;
        for(int i=0;i<n;i++){
            for(int j=0;j<n-i-1;j++){
                if(less(nums[j+1],nums[j])) {
                    swap(nums,j,j+1);
                }
            }
        }
    }

    public static void main(String[] args){
        Integer[] arr={
                5,1,8,7,10,6,9,5,20,3,0
        };
        Sort<Integer> sort=new Buddle<Integer>();
        sort.sort(arr);
        ArrayUtils.printArray(arr);
    }
}
```

## python

```python
data = [16, 25, 39, 27, 12, 8, 45, 63]

for i in range(len(data) - 1, 0, -1):
    for j in range(i):
        if data[j] > data[i]:
            data[i], data[j] = data[j], data[i]

print(data)
```


---

# 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/pai-xu-lei/2.mao-pao-pai-xu.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.
