排序:冒泡排序

package PaiXu;

/**
 * 冒泡排序n^2/2
 * 
 * @author Youjc
 * @date Mar 27, 2018
 */
public class MaoPaoPaiXu {

	public MaoPaoPaiXu() {
		// TODO Auto-generated constructor stub
		int[] a = new int[] { 5, 9, 3, 1, 2, 8, 4, 7, 6 };
		MaoPaoSort(a);
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}
	}

	public void MaoPaoSort(int[] a) {
		if ((a == null) || (a.length == 0))
			return;
		int temp = 0;
		for (int i = a.length - 1; i > 0; --i) {
			for (int j = 0; j < i; ++j) {
				if (a[j + 1] < a[j]) {
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
	}

	public static void main(String[] args) {
		new MaoPaoPaiXu();
	}

}

猜你喜欢

转载自blog.csdn.net/youjianchi/article/details/79711031