package Sort8;
import java.util.Arrays;
public class BubbleSort {
public static void bubbleSort(int[] a,int n) {
//控制比较多少轮
for(int i = 0;i<n-1;i++) {
//控制比较的次数
for(int j = 0;j<n-1-i;j++) {
if(a[j]>a[j+1]) {
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
public static void main(String[] args) {
int a[] = new int[] {5,7,2,9,4,1,0,5,7};
bubbleSort(a,a.length);
System.out.println(Arrays.toString(a));
//[0, 1, 2, 4, 5, 5, 7, 7, 9]
}
}
冒泡排序Java实现
最新推荐文章于 2020-09-29 11:37:04 发布