冒泡排序算法

数组排序之冒泡排序

/**
 * 冒泡排序算法
 * @author	努力Coding
 * @version
 * @data
 */
public class BubbleSort {

	public static void main(String[] args) {
		int[] array = {6,3,8,2,9,1};
		System.out.println("排序前的数组为:");
		for(int before : array) {//foreach遍历
			System.out.print(before + ",");
		}
		
		/*核心算法*/
		for(int i = 0; i < array.length - 1; i++) {
			for(int j = 0; j < array.length - i - 1; j++) {
				if(array[j] < array[j+1]) {//降序排列
					int temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;
				}
			}
		}
		
		System.out.println();
		System.out.println("排序后的数组为:");
		for(int over : array) {//foreach遍历
			System.out.print(over + ",");
		}
		
	}

}
posted @ 2020-01-07 17:03  笔架山Code  阅读(77)  评论(0)    收藏  举报