> For the complete documentation index, see [llms.txt](https://jun-wang.gitbook.io/learnjava/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jun-wang.gitbook.io/learnjava/ji-shu-xue-xi/suan-fa/pai-xu-suan-fa/mao-pao-pai-xu.md).

# 冒泡排序

## 冒泡排序思路：

通过比较和交换

1. 从开头对相邻的两个数据进行比较
2. 如果前面的数据大于后面的数据，则交换位置，经过一轮比较后，最大的数据就放在了最后面
3. 依次遍历整个数据，把所有的数据排序

**时间复杂度：O(n^2) 最好情况：O(n) 最坏情况：O(n^2)**

**空间复杂度：O(1)**

**稳定性：稳定**

**复杂性：简单**

## Java实现

```java
package com.wangjun.arithmetic;

import java.util.Arrays;

/**
 * 冒泡排序
 * 
 * @author wangjun
 * @email scuwangjun@hotmail.com
 * @time 2018年4月5日 下午7:15:13
 */
public class SortBubble {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] arr = { 3, 1, 2, 4, 5, 6, 3, 1, 2, 4, 3};
        doBubble(arr);
        System.out.println(Arrays.toString(arr));
    }

    /*
     * 冒泡排序的实现 
     * 从小到大，冒到最后面 
     * 第一个for循环次数，可以理解每个i的值是每次遍历最大值放的位置 第二个for进行冒泡交换
     */
    public static void doBubble(int[] arr) {
        int len = arr.length;
        for(int i = 0; i < len; i++) {
            boolean isChange = false;
            for(int j = 0; j < len - i - 1; j++) {
                if(arr[j] > arr[j+1]) {
                    arr[j] = arr[j] + arr[j+1];
                    arr[j+1] = arr[j] - arr[j+1];
                    arr[j] = arr[j] - arr[j+1];
                    isChange = true;
                }
            }
            if(!isChange) {
                break;
            }
        }
    }

    /*
     * 冒到最前面
     */
    public static void doBubble2(int a[]) {
        int temp;
        int count = 0;
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                    count++;
                }
            }
        }
        System.out.println(count);
    }

}
```
