点击查看代码
package Sort;
import java.util.Arrays;
//每一轮外层循环都会固定一个最大或最小的数到后面去,然后内层循环继续从0开始到最后未排序的数组末项
public class BubbleSort {
public static void main(String[] args) {
int[] a = {2, 3, 0, 8, 4, 10, 6, 5};
// 升序
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) { // 注意这里,内层循环的条件是 a.length - i - 1
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(a));
int []c ={2,3,0,8,4,10,6,5};
for(int i=c.length-1;i>0;i--){
findMax(c,i);
}
System.out.println(Arrays.toString(c));
}
public static void findMax(int []a,int n){
for(int i=0;i<n;i++){
if(a[i]>a[i+1]){
swap(a,i,i+1);
}
}
}
public static void swap(int []a,int n,int n2){
int temp=a[n];
a[n]=a[n2];
a[n2]=temp;
}
}
~! [地址]https://blog.csdn.net/luo_666666/article/details/141338634?spm=1001.2014.3001.5501
标签:temp,int,void,冒泡排序,length,static,public From: https://www.cnblogs.com/hutbluo/p/18416551