冒泡排序(java)

来源:互联网 发布:如何举报淘宝店铺盗图 编辑:程序博客网 时间:2024/05/17 22:37
/* * 冒泡排序算法 * 8,9,5,2,7 * 冒泡排序: *  */public class BubbleSort {    public static void main(String[] args){        int[] a={8,9,5,2,7};        //开始排序        for(int i=a.length-1;i>0;i--){            for(int j=0;j<i;j++){                //如果左边的变量大于右边的变量,让其相互换值                if(a[j]>a[j+1]){                int temp = a[j];                a[j] =a[j+1];                a[j+1] = temp;                               }            }        }        //遍历数组        for(int i=0;i<a.length;i++){            System.out.println(a[i]);        }    }}
0 0
原创粉丝点击