冒泡排序及其优化

import java.util.Arrays;

public class bobbleSort {

    public static void main(String[] args) {

        int[] arr = {2,6,3,7,4,1,8,5,0,9};
        //          {2,3,6,4,1,7,5,0,8,9}
        int temp;
        boolean result = true;
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr.length-i-1;j++){
                if(arr[j]>arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    result = false;
                }
          System.out.println("第"+i+"趟,第"+j+"次:"+Arrays.toString(arr));  }
if(result){ System.out.println(Arrays.toString(arr)); break; }else { result = true; } } } }

 

posted @ 2023-03-02 22:59  Chaman囍  阅读(21)  评论(0)    收藏  举报