package com.itjob.algorithm;

public class BubbleSort {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] array = {2, 5, 3, 1, 9, 8, 10};
		bubbleSort(array);
		for (int i = 0; i < array.length; i++)
		{
			System.out.println(array[i]);
		}
	}

	/**
	 * 冒泡排序算法
	 * @param array
	 */
	static void bubbleSort(int[] array) {
		int out, in, len;
		len = array.length;
		// 外循环记录冒泡次数
		for (out = len - 1; out > 1; out--) {
			boolean flag = false;
			// 进行冒泡
			for (in = 0; in < out; in++) {
				if (array[in] > array[in + 1]) {
					int temp = array[in];
					array[in] = array[in + 1];
					array[in + 1] = temp;
					flag = true;
				}
			}
			if (!flag) {
				break;
			}
		}
	}
}
