---
title: "排序篇:bubble"
author: "shifulong"
date: "2015-11-08T21:41:04Z"
tags: []
categories: ["编程语言"]
slug: "bubble"
draft: false
lastmod: "2015-11-08T21:41:04Z"
id: 2255603
url: "https://shifulong.iteye.com/blog/2255603"
---

```
public static void bubbleSort() {
    int[] arr = {0, 5, 6, 333, 5, 8, 999, 7, 7, 5, 45, 3};
    for (int i = arr.length - 1; i > 0; i--) {
        for (int j = arr.length - 1; j > arr.length - i - 1; j--) {
            if (arr[j] < arr[j - 1]) {
                swap(arr, j, j - 1);
            }
        }
    }
    System.out.println(Arrays.toString(arr));
}

public static void swap(int arr[], int j, int i) {
    int tmp = arr[j];
    arr[j] = arr[i];
    arr[i] = tmp;
}
```