冒泡排序:平均時間復(fù)雜度O(n^2) 空間復(fù)雜度O(l) 穩(wěn)定
/**
*
* @Description:實(shí)現(xiàn)對數(shù)組的冒泡排序
* @author: YAO
* @date: 2019年10月11日 下午10:46:01
*/
public class BubbleSort {
public static void main(String[] args) {
// 定義一個亂序的數(shù)組
int[] array = new int[] { 29, 23, -1, 1, 23, 56, -90 };
// 對整型數(shù)組進(jìn)行冒泡排序
// 進(jìn)行array.length-1次排序
for (int i = 0; i < array.length - 1; i++) {
// 每循環(huán)一次最后一個元素就不用進(jìn)行兩兩比較
for (int j = 0; j < array.length - i - 1; j++) {
// 從大到小排序
if (array[j + 1] > array[j]) {
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
// 遍歷數(shù)組
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "\t");
}
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者