2.比较相邻的元素。不符合条件,就交换他们两个。
冒泡排序最坏情况的时间复杂度是O(n²)
冒泡排序属于交换排序,稳定,空间复杂度为O(1)
public class maopao {
public int[] maopao(int shuzu[],int n) {//n为数组长度;
for (int j = 0; j< n-1;j++) {
for (int i = 0; i < n-j-1; i++) {
int max;
if(shuzu[i]>shuzu[i+1]){//两两相比,若i项数值大于i+1项,则交换他们的位置;
max=shuzu[i];//将数值大的i项赋值给max;
shuzu[i]=shuzu[i+1];//将i+1项赋值给i项;
shuzu[i+1]=max;//将max赋值给i+1项;
}
}
}
return shuzu;
}
}