java版数据结构与算法—冒泡排序

/**
 * 冒泡排序规则:
 * 1.比较两个相邻对象
 * 2.如果左边的大于右边的,则调换位置
 * 3.向右移动一个位置,比较接下来的两个对象
 * 时间复杂度:O(log n^2)
 */
class ArrayBubble {
    public static void bubbleSort(int arr[]){
        for(int i=arr.length - 1; i>1;i--){
            for(int j=0;j<i;j++){
                if(arr[j] > arr[j+1]){
                    int temp = arr[j+1];
                    arr[j+1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args){
        int arr[] = {32,34,65,21,23,33,45,1,66,22,99,11,109};
        bubbleSort(arr);
        for(int i = 0; i<arr.length;i++){
            System.out.print(arr[i] + " ");
        }
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_38799368/article/details/84037795