排序算法之冒泡排序

来源:互联网 发布:苹果mac锁屏快捷键 编辑:程序博客网 时间:2024/06/10 09:16

时间复杂度:平均O(n²)  最好O(n)  最坏O(n²)

空间复杂度:O(1)

稳定性:稳定

特点:n小时较好

public class BubbleSort {public static void main(String[] args) {int[] a = { 2, 7, 8, 3, 1, 6, 9, 0, 5, 4 };bubbleSort(a);for (int n : a) {System.out.print(n + " ");}}public static void bubbleSort(int[] a) {if (a == null) {return;}int i, j, temp;int l = a.length;for (i = 0; i < l - 1; i++) {for (j = l - 1; j > i; j--) {if (a[j] < a[j - 1]) {temp = a[j];a[j] = a[j - 1];a[j - 1] = temp;}}}}}


0 0
原创粉丝点击