交换排序之-冒泡排序

package demo4;

import java.util.Arrays;

public class BubbleSort {

    public static void main(String[] args){
        int[] arr = new int[]{5,7,2,9,4,1,0,5,7};
        System.out.println(Arrays.toString(arr));
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));


    }

    /**
     *
     * 5,7,2,9,4,1,0,5,7  共需比较length-1次
     *
     */

    public static void bubbleSort(int[] arr){
        //控制共比较多少轮
            for(int i = 0;i<arr.length-1;i++){
                //控制比较的次数
                for(int j =0;j<arr.length-1-i;j++ ){
                    if(arr[j] > arr[j+1]){
                        int temp = arr[j+1];
                        arr[j+1] = arr[j];
                        arr[j] = temp;
                    }

                }


            }

    }
}

猜你喜欢

转载自blog.csdn.net/nowfuture/article/details/89314598