java冒泡排序

public class maopao {
    public static void main(String[] args) {
        int a[] = {45, 78, 26, 12, 45};
        sort(a);
    }
    public static void sort(int[] a) {
        int temp = 0;
        for(int i = 0;i < a.length-1;i++){
            for (int j = 0;j < a.length-1-i;j++){
                if (a[j] > a[j+1]){
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        for (int i : a){
            System.out.println(i + " ");
        }
    }
}
public class maopao {
    public static void main(String[] args) {
       int temp = 0;
       int[] a = {45, 78, 26, 12, 45};
       for (int j=0;j<a.length-1;j++){
           for (int i = 0; i < a.length-1-j;i++){
               if (a[i]>a[i+1]){
                   temp = a[i];
                   a[i] = a[i+1];
                   a[i+1] = temp;
               }
           }
       }
       for (int i = 0;i<a.length;i++){
           System.out.print(a[i] + " ");
       }
    }
}

 

posted @ 2022-07-25 09:43  寒无衣  阅读(27)  评论(0)    收藏  举报