import java.util.Arrays;public class SortDemo { public static void main(String[] args) { int[] ary=new int[]{8,2,5,7,3,1}; //从小到大排序,并输出 //Arrays.sort(ary); bubble(ary); System.out.println(Arrays.toString(ary)); } public static void bubble(int[] ary){ //冒泡排序 for(int i=0;i<ary.length-1;i++){ //i是比几轮,(length-1)轮 for(int j=0;j<ary.length-1-i;j++){ //-i意思是最大的数在第一轮就到了最底下,下一轮就不用比了 if(ary[j]>ary[j+1]){ int t=ary[j]; ary[j]=ary[j+1]; ary[j+1]=t; } } } }}
来源:https://www.cnblogs.com/jerry201907/p/12448243.html