冒泡排序,从后往前两两比较相邻元素的值,一个排序可以把一个元素放在最终位置上,最大或者最小,是一种稳定的算法,平均时间复杂度O(n*n)
public class maopao {
public static void main(String[] args) {
int[] str = {1,3,2,9,13,11,25,4,7,5};
for (int i = 0; i < str.length-1; i++) {
int temp;
if (str[i]>str[i+1]){
temp = str[i];
str[i]=str[i+1];
str[i+1]=temp;
}
}
for (int j = 0; j < str.length; j++) {
System.out.print(str[j]+" ");
}
}
}
运行结果:

5129

被折叠的 条评论
为什么被折叠?



