冒泡排序
作者:互联网
分为内层循环和外层循环,
每次内层循环从左到右两两相比较,将最小的数放到最右边,一次内层循环走完放置出一个最小数,放置出的最小数就不需要再参与比较了,不要去动它。
外层循环决定要找出的最小数的个数。
//降序冒泡排序 public class demo3 { public static void main(String[] args) { int[] a = {1, 2, 7, 8, 5, 9, 6, 3,}; int[] b= sort(a); System.out.println(Arrays.toString(b)); } public static int[] sort(int[] a) { boolean flag=false; 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 + 1] > a[j]) { temp = a[j + 1]; a[j + 1] = a[j]; a[j] = temp; flag=true; } } if (flag==false){ break; } } return a; } }
标签:temp,int,冒泡排序,内层,flag,循环,public 来源: https://www.cnblogs.com/guojianglong/p/16492362.html