Bubble Sort

 

package com.fairy.test;

public class BubbleSort {
	public static void bubbleSort(int[] a) {
		for (int i = 0; i < a.length; i++) {
			for (int j = a.length - 1; j > i; j--) {
				if (a[j] < a[j - 1]) {
					int temp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = temp;
				}
			}
		}
		printArray(a);
	}

	public static void printArray(int[] a) {
		for (int i : a) {
			System.out.print(i + " ");
		}
		System.out.println();
	}

	public static void main(String[] args) {
		int[] a = { 67, 89, 87, 69, 90, 100, 75, 90 };
		bubbleSort(a);
	}
}

你可能感兴趣的:(Bubble)