java - 冒泡排序求最值

public class Bubble3 {

    public static void main(String[] args) {
        
        int[] arr;
        arr = new int[]{2,3,6,1};
        
        Bubble3 Bu3 = new Bubble3();
        Bu3.jiaohuan(arr);
        
    }
    
    void jiaohuan(int[] arr){
        
        for(int i=0;i<arr.length-1;i++){
            // < 求最小值  ,  >求最大值
            if(arr[i]<arr[i+1]){    
                arr[i] = arr[i]^arr[i+1];
                arr[i+1] = arr[i+1]^arr[i];
                arr[i] = arr[i]^arr[i+1];
            }
            
        }
        
        for(int item : arr){
            System.out.println(item);
        }
        
        System.out.println("最值 : "+arr[arr.length-1]);
    }

}

 

posted @ 2020-09-16 09:27  武卡卡  阅读(178)  评论(0编辑  收藏  举报