Java之冒泡排序

1. 输出数组元素的最大值

package com.vip.array;

public class ArrayDemo2 {
    public static void main(String[] args) {
        //求数组元素的最大值
        int[] a = {34,23,12,45,78,9,55};
        int max = a[0];
        for(int i=1;i<a.length;i++){
            if(max<a[i]){
                max = a[i];
            }
        }
        System.out.println("数组中的最大值为:"+max);
    }
}

2. 冒泡排序

package com.vip.array;

public class SortDemo {
    public static void main(String[] args) {
        int[] a = {34,23,12,45,78,9,55};
        int temp;
        for(int i=0;i<a.length-1;i++){
            for(int j=0;j<a.length-i-1;j++){
                if(a[j]>a[j+1]){
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }
    }
}
原文地址:https://www.cnblogs.com/mpp0905/p/11524329.html