数组排序之冒泡排序
/**
* 冒泡排序算法
* @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 + ",");
}
}
}
原文:https://www.cnblogs.com/Zhouge6/p/12162555.html