Sorting-JAVA Implementation [2] Bubble Sort

package org.lion.euler.study.sort;

/**
 * Bubble Sort
 * <pre>
 * Principle: Compare two adjacent values, and swap the one with the larger one on the left until the last one.
 * </pre>
 * @author lion
 *
 */
public class BubbleSort extends AbstractSort {

	@Override
	public void sort(Integer[] array) {
		if(array == null){
			return;
		}
		int length = array.length;
		if(length == 0){
			return;
		}

		for (int i = array.length - 1; i > 0; i--) {
			for (int j = 1; j <= i; j++) {
				if(array[j - 1] > array[j]){
					swap(array, j - 1, j);
				}
			}
		}
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325981373&siteId=291194637